#AGC030F. [AGC030F] Permutation and Minimum

[AGC030F] Permutation and Minimum

Score : 16001600 points

Problem Statement

There is a sequence of length 2N2N: A1,A2,...,A2NA_1, A_2, ..., A_{2N}. Each AiA_i is either 1-1 or an integer between 11 and 2N2N (inclusive). Any integer other than 1-1 appears at most once in Ai{A_i}.

For each ii such that Ai=1A_i = -1, Snuke replaces AiA_i with an integer between 11 and 2N2N (inclusive), so that Ai{A_i} will be a permutation of 1,2,...,2N1, 2, ..., 2N. Then, he finds a sequence of length NN, B1,B2,...,BNB_1, B_2, ..., B_N, as Bi=min(A2i1,A2i)B_i = min(A_{2i-1}, A_{2i}).

Find the number of different sequences that B1,B2,...,BNB_1, B_2, ..., B_N can be, modulo 109+710^9 + 7.

Constraints

  • 1N3001 \leq N \leq 300
  • Ai=1A_i = -1 or 1Ai2N1 \leq A_i \leq 2N.
  • If Ai1,Aj1A_i \neq -1, A_j \neq -1, then AiAjA_i \neq A_j. (iji \neq j)

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 ...... A2NA_{2N}

Output

Print the number of different sequences that B1,B2,...,BNB_1, B_2, ..., B_N can be, modulo 109+710^9 + 7.

3
1 -1 -1 3 6 -1
5

There are six ways to make Ai{A_i} a permutation of 1,2,...,2N1, 2, ..., 2N; for each of them, Bi{B_i} would be as follows:

  • $(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 2, 4, 3, 6, 5)$: (B1,B2,B3)=(1,3,5)(B_1, B_2, B_3) = (1, 3, 5)
  • $(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 2, 5, 3, 6, 4)$: (B1,B2,B3)=(1,3,4)(B_1, B_2, B_3) = (1, 3, 4)
  • $(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 3, 6, 5)$: (B1,B2,B3)=(1,2,5)(B_1, B_2, B_3) = (1, 2, 5)
  • $(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 5, 3, 6, 2)$: (B1,B2,B3)=(1,3,2)(B_1, B_2, B_3) = (1, 3, 2)
  • $(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 5, 2, 3, 6, 4)$: (B1,B2,B3)=(1,2,4)(B_1, B_2, B_3) = (1, 2, 4)
  • $(A_1, A_2, A_3, A_4, A_5, A_6) = (1, 5, 4, 3, 6, 2)$: (B1,B2,B3)=(1,3,2)(B_1, B_2, B_3) = (1, 3, 2)

Thus, there are five different sequences that B1,B2,B3B_1, B_2, B_3 can be.

4
7 1 8 3 5 2 6 4
1
10
7 -1 -1 -1 -1 -1 -1 6 14 12 13 -1 15 -1 -1 -1 -1 20 -1 -1
9540576
20
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 -1 -1 -1 -1 -1 7 -1 -1 -1 -1 -1 -1 -1 -1 -1 34 -1 -1 -1 -1 31 -1 -1 -1 -1 -1 -1 -1 -1
374984201