P19344 [CodeChef] GIFTPAIRS
提高+
题目描述

"We’ve already come too far to end this now".

N players advance to the fifth round and the game is going to be Hopscotch. Every player has to select one mannequin before the start of the game.

Players are numbered 1 to N. You are given an array A of size N, where
* If A_{i} = i, the $i^{\text{th}}$ player selects their own mannequin.
* If A_{i} = j where $j \neq i$, the $i^{\text{th}}$ player decides the mannequin for the $j^{\text{th}}$ player.
* If A_{i} = 0, the $i^{\text{th}}$ player hasn’t decided yet.

The VIPs want to know the number of ways that the undecided players can select mannequins such that everyone gets a mannequin, and no undecided players select their own mannequin.

It is guaranteed that every (non-zero) element in A is unique, i.e, if 1 ≤ i, j ≤ N are such that $i \neq j, A_{i} > 0$, and A_{j} > 0, then $A_{i} \neq A_{j}$.

Since the answer can be very large, print it modulo 10^{9} + 7.

------ Input Format ------

  • The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N denoting the number of players.
  • The second line contains N space-separated integers, the $i^{\text{th}}$ of which is A_{i} as described in the statement above.

------ Output Format ------

For every test case, print one line containing the number of ways undecided players can select mannequins for others such that everyone receives a mannequin, modulo 10^{9}+7.

------ Constraints ------

$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{3}$
$0 ≤ A_{i} ≤ N$
- The non-zero elements of $A$ are distinct.

----- Sample Input 1 ------
3
5
1 2 3 4 5
6
3 6 1 0 0 0
10
0 0 0 0 0 0 0 0 0 0
----- Sample Output 1 ------
0
3
1334961
----- explanation 1 ------
Test Case 1: There is no undecided player, so the number of ways is $0$.

Test Case 2: There are $3$ ways for the last $3$ players to decide mannequins, all of which are listed below:
- $4 \rightarrow 2 , 5 \rightarrow 4, 6 \rightarrow 5$
- $4 \rightarrow 5 , 5 \rightarrow 4, 6 \rightarrow 2$
- $4 \rightarrow 5 , 5 \rightarrow 2, 6 \rightarrow 4$

where $a \rightarrow b$ means that $a$ selects the mannequin for $b$.

输入格式

见题面

输出格式

见题面

样例输入
3 5 1 2 3 4 5 6 3 6 1 0 0 0 10 0 0 0 0 0 0 0 0 0 0
样例输出
0 3 1334961