Iahub is so happy about inventing bubble sort graphs that he's staying all day long at…
- User
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7). Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5 -1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
Assistant
pythonMOD = 10**9 + 7 n = int(input()) a = list(map(int, input().split())) existing = set() P = [] for idx in range(n): if a[idx] == -1: P.append(idx + 1) # Convert to 1-based index else: existing.add(a[idx]) missing = [] for x in range(1, n + 1): if x not in existing: missing.append(x) m = len(P) c = len(set(P) & set(missing)) # Precompute factorial and inverse factorial up to m max_fact = m fact = [1] * (max_fact + 1) for i in range(1, max_fact + 1): fact[i] = fact[i - 1] * i % MOD inv_fact = [1] * (max_fact + 1) inv_fact[max_fact] = pow(fact[max_fact], MOD - 2, MOD) for i in range(max_fact - 1, -1, -1): inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD def comb(n_, k_): if k_ < 0 or k_ > n_: return 0 return fact[n_] * inv_fact[k_] % MOD * inv_fact[n_ - k_] % MOD result = 0 for k in range(0, c + 1): term = comb(c, k) * fact[m - k] % MOD if k % 2 == 1: term = (-term) % MOD result = (result + term) % MOD print(result)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.