#P15987. [PA 2026] 多重桥牌 / Multi-brydż

[PA 2026] 多重桥牌 / Multi-brydż

Problem Description

In the game of multiple bridge, there are two teams (we call them the "Keban-zhe" and the "Algorithmists"), each consisting of nn players.

The players sit around a round table, with positions numbered from 11 to 2n2n. The Keban-zhe sit in odd positions, and the Algorithmists sit in even positions. The game uses 4n4n cards with face values 1,2,3,…,4n1, 2, 3, \ldots, 4n. Each player holds two of these cards at the start of the game. Every player knows the cards held by all other players.

The game consists of two rounds. In the first round, the player at a random position ii plays first and plays one of their two cards. Then, the players at positions $(i \bmod 2n) + 1,\ (i+1 \bmod 2n) + 1,\ \ldots,\ (i+2n-2 \bmod 2n) + 1$ play in order (each plays one of their two cards). The team of the player who played the highest-valued card gets 11 point. In the second round, all players play their remaining card. Again, the team of the player who played the highest-valued card gets 11 point.

The input gives a sequence of 2n2n integers a1,…,a2na_1, \ldots, a_{2n} describing the relationship between the game outcome and the starting player. Specifically, for 1≤i≤2n1 \le i \le 2n, if the player at position ii plays first and all players use optimal strategies, then the Algorithmists team gets exactly aia_i points.

Compute the number of different dealing arrangements consistent with the given outcome sequence, and output this number modulo 109+710^9 + 7. If for some position ii and some card value xx, the player at position ii holds the card with value xx in one arrangement but does not hold it in another, then these two dealing arrangements are considered different.

You need to solve this problem for tt independent test cases.

Input Format

The first line contains an integer tt (1≤t≤10001 \le t \le 1000), the number of test cases.

For each test case, the first line contains an integer nn (1≤n≤1061 \le n \le 10^6), the number of players on each team.

The second line contains a sequence of 2n2n integers a1,…,a2na_1, \ldots, a_{2n} (0≤ai≤20 \le a_i \le 2). The value aia_i indicates the number of points the Algorithmists team gets when the player at position ii plays first.

The sum of nn over all test cases does not exceed 10610^6.

Output Format

Output tt lines. The jj-th line should contain one integer: the number of dealing arrangements consistent with the outcome sequence of the jj-th test case, modulo 109+710^9 + 7.

4
2
1 0 1 0
1
0 2
3
1 0 0 1 1 0
7
1 1 1 1 1 1 1 1 1 1 1 1 1 1
24
0
0
256223893

Hint

Sample Explanation

In the first test case, one dealing arrangement consistent with the given input outcome sequence is: the first player holds cards 44 and 66, the second player holds 33 and 77, the third player holds 22 and 88, and the fourth player holds 11 and 55.

In the second and third test cases, there is no dealing arrangement consistent with the given input outcome sequence.

Translated by ChatGPT 5