## Where is the Pizza? solution codeforces

While searching for the pizza, baby Hosssam came across two permutations aa and bb of length nn.

Recall that a permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Baby Hosssam forgot about the pizza and started playing around with the two permutations. While he was playing with them, some elements of the first permutation got mixed up with some elements of the second permutation, and to his surprise those elements also formed a permutation of size nn.

Specifically, he mixed up the permutations to form a new array cc in the following way.

- For each ii (1≤i≤n1≤i≤n), he either made ci=aici=ai or ci=bici=bi.
- The array cc is a permutation.

## Where is the Pizza? solution codeforces

You know permutations aa, bb, and values at some positions in cc. Please count the number different permutations cc that are consistent with the described process and the given values. Since the answer can be large, print it modulo 109+7109+7.

It is guaranteed that there exists at least one permutation cc that satisfies all the requirements.

The first line contains an integer tt (1≤t≤1051≤t≤105) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the length of the permutations.

The next line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the first permutation.

The next line contains nn distinct integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤n1≤bi≤n) — the second permutation.

The next line contains nn distinct integers d1,d2,…,dnd1,d2,…,dn (didi is either 00, aiai, or bibi) — the description of the known values of cc. If di=0di=0, then there are no requirements on the value of cici. Otherwise, it is required that ci=dici=di.

It is guaranteed that there exists at least one permutation cc that satisfies all the requirements.

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055⋅105.

For each test case, print the number of possible permutations cc, modulo 109+7109+7.

input

## Where is the Pizza? solution codeforces

9 7 1 2 3 4 5 6 7 2 3 1 7 6 5 4 2 0 1 0 0 0 0 1 1 1 0 6 1 5 2 4 6 3 6 5 3 1 4 2 6 0 0 0 0 0 8 1 6 4 7 2 3 8 5 3 2 8 1 4 5 6 7 1 0 0 7 0 3 0 5 10 1 8 6 2 4 7 9 3 10 5 1 9 2 3 4 10 8 6 7 5 1 9 2 3 4 10 8 6 7 5 7 1 2 3 4 5 6 7 2 3 1 7 6 5 4 0 0 0 0 0 0 0 5 1 2 3 4 5 1 2 3 4 5 0 0 0 0 0 5 1 2 3 4 5 1 2 3 5 4 0 0 0 0 0 3 1 2 3 3 1 2 0 0 0

output

4 1 2 2 1 8 1 2 2

## Where is the Pizza? solution codeforces

In the first test case, there are 44 distinct permutation that can be made using the process: [2,3,1,4,5,6,7][2,3,1,4,5,6,7], [2,3,1,7,6,5,4][2,3,1,7,6,5,4], [2,3,1,4,6,5,7][2,3,1,4,6,5,7], [2,3,1,7,5,6,4][2,3,1,7,5,6,4].

In the second test case, there is only one distinct permutation that can be made using the process: [1][1].

In the third test case, there are 22 distinct permutation that can be made using the process: [6,5,2,1,4,3][6,5,2,1,4,3], [6,5,3,1,4,2][6,5,3,1,4,2].

In the fourth test case, there are 22 distinct permutation that can be made using the process: [1,2,8,7,4,3,6,5][1,2,8,7,4,3,6,5], [1,6,4,7,2,3,8,5][1,6,4,7,2,3,8,5].

In the fifth test case, there is only one distinct permutation that can be made using the process: [1,9,2,3,4,10,8,6,7,5][1,9,2,3,4,10,8,6,7,5].