Problem Description
You are given an integer sequence S=s1s2⋯s3n of length 3n, with values in [0,3]. You first need to replace every 0 in S with any integer in [1,3], obtaining a sequence T=t1t2⋯t3n. Then you need to provide n integer sequences of length 3, ${\{ a_{i, 1}, a_{i, 2}, a_{i, 3} \}}_{1 \le i \le n}$, such that:
- ∀1≤i≤n, 1≤ai,1<ai,2<ai,3≤3n;
- ∀(i1,j1)=(i2,j2), ai1,j1=ai2,j2;
- ∀1≤i≤n, {tai,1,tai,2,tai,3} is a permutation of {1,2,3}, and the number of inversions is odd.
Two solutions are considered essentially different if and only if the sequence T is different, or there exists some ai,j (1≤i≤n, 1≤j≤3) that is different. Find the number of essentially different solutions to the above operations, modulo (109+7).
This problem has multiple test cases. The first line of the input contains a positive integer C indicating the number of test cases.
For each test case, the first line contains an integer n. The next line contains a string of length 3n describing the sequence S.
For each test case, output one line containing one integer: the number of solutions modulo (109+7).
5
1
123
1
100
1
000
2
321321
2
000001
0
1
3
6
60
Hint
[Sample Explanation #1]
In the first three test cases, n=1, so {a1,1,a1,2,a1,3}={1,2,3}.
For the first test case, the only possible T is 123, and the number of inversions of {1,2,3} is 0, which is invalid, so there is no solution.
For the second test case, T=123 is invalid, while for T=132, the number of inversions of {1,3,2} is 1, which is valid, so there is one solution.
For the third test case, choosing T=132, T=213, or T=321 gives three valid solutions.
For the fourth test case, T=321321, and there are six solutions as follows:
- {a1,1,a1,2,a1,3}={1,2,3}, {a2,1,a2,2,a2,3}={4,5,6}
- {a1,1,a1,2,a1,3}={4,5,6}, {a2,1,a2,2,a2,3}={1,2,3}
- {a1,1,a1,2,a1,3}={1,2,6}, {a2,1,a2,2,a2,3}={3,4,5}
- $\{ a_{1, 1}, a_{1, 2}, a_{_{i, 3}} \} = \{ 3, 4, 5 \}$, {a2,1,a2,2,a2,3}={1,2,6}
- {a1,1,a1,2,a1,3}={1,5,6}, {a2,1,a2,2,a2,3}={2,3,4}
- {a1,1,a1,2,a1,3}={2,3,4}, {a2,1,a2,2,a2,3}={1,5,6}
[Sample #2]
See problem/problem2.in and problem/problem2.ans in the attachment.
In this sample, the n of the five test cases are 3,5,8,12,17, respectively.
[Sample #3]
See problem/problem3.in and problem/problem3.ans in the contestant directory.
This sample satisfies special property A, and the n of the five test cases are 2,4,7,15,19, respectively.
[Constraints]
For all testdata, 1≤C≤5, 1≤n≤19. The length of the string S is 3n, and it consists only of 0,1,2,3.
| Test Point ID |
n≤ |
Special Property |
| 1 |
None |
| 2 |
| 3 |
| 4 |
5 |
A |
| 5 |
7 |
None |
| 6 |
10 |
| 7 |
13 |
A |
| 8 |
16 |
None |
| 9 |
18 |
| 10 |
19 |
Special property A: the string S consists of all 0.
[Hint]
Please pay attention to the program’s memory usage.
Translated by ChatGPT 5