#P8366. [LNOI2022] 题

[LNOI2022] 题

Problem Description

You are given an integer sequence S=s1s2s3nS = s_1 s_2 \cdots s_{3 n} of length 3n3 n, with values in [0,3][0, 3]. You first need to replace every 00 in SS with any integer in [1,3][1, 3], obtaining a sequence T=t1t2t3nT = t_1 t_2 \cdots t_{3 n}. Then you need to provide nn integer sequences of length 33, ${\{ a_{i, 1}, a_{i, 2}, a_{i, 3} \}}_{1 \le i \le n}$, such that:

  • 1in\forall 1 \le i \le n, 1ai,1<ai,2<ai,33n1 \le a_{i, 1} < a_{i, 2} < a_{i, 3} \le 3 n;
  • (i1,j1)(i2,j2)\forall (i_1, j_1) \ne (i_2, j_2), ai1,j1ai2,j2a_{i_1, j_1} \ne a_{i_2, j_2};
  • 1in\forall 1 \le i \le n, {tai,1,tai,2,tai,3}\{ t_{a_{i, 1}}, t_{a_{i, 2}}, t_{a_{i, 3}} \} is a permutation of {1,2,3}\{ 1, 2, 3 \}, and the number of inversions is odd.

Two solutions are considered essentially different if and only if the sequence TT is different, or there exists some ai,ja_{i, j} (1in1 \le i \le n, 1j31 \le j \le 3) that is different. Find the number of essentially different solutions to the above operations, modulo (109+7)({10}^9 + 7).

Input Format

This problem has multiple test cases. The first line of the input contains a positive integer CC indicating the number of test cases.

For each test case, the first line contains an integer nn. The next line contains a string of length 3n3 n describing the sequence SS.

Output Format

For each test case, output one line containing one integer: the number of solutions modulo (109+7)({10}^9 + 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=1n = 1, so {a1,1,a1,2,a1,3}={1,2,3}\{ a_{1, 1}, a_{1, 2}, a_{1, 3} \} = \{ 1, 2, 3 \}.

For the first test case, the only possible TT is 123123, and the number of inversions of {1,2,3}\{ 1, 2, 3 \} is 00, which is invalid, so there is no solution.

For the second test case, T=123T = 123 is invalid, while for T=132T = 132, the number of inversions of {1,3,2}\{ 1, 3, 2 \} is 11, which is valid, so there is one solution.

For the third test case, choosing T=132T = 132, T=213T = 213, or T=321T = 321 gives three valid solutions.

For the fourth test case, T=321321T = 321321, and there are six solutions as follows:

  • {a1,1,a1,2,a1,3}={1,2,3}\{ a_{1, 1}, a_{1, 2}, a_{1, 3} \} = \{ 1, 2, 3 \}, {a2,1,a2,2,a2,3}={4,5,6}\{ a_{2, 1}, a_{2, 2}, a_{2, 3} \} = \{ 4, 5, 6 \}
  • {a1,1,a1,2,a1,3}={4,5,6}\{ a_{1, 1}, a_{1, 2}, a_{1, 3} \} = \{ 4, 5, 6 \}, {a2,1,a2,2,a2,3}={1,2,3}\{ a_{2, 1}, a_{2, 2}, a_{2, 3} \} = \{ 1, 2, 3 \}
  • {a1,1,a1,2,a1,3}={1,2,6}\{ a_{1, 1}, a_{1, 2}, a_{1, 3} \} = \{ 1, 2, 6 \}, {a2,1,a2,2,a2,3}={3,4,5}\{ a_{2, 1}, a_{2, 2}, a_{2, 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}\{ a_{2, 1}, a_{2, 2}, a_{2, 3} \} = \{ 1, 2, 6 \}
  • {a1,1,a1,2,a1,3}={1,5,6}\{ a_{1, 1}, a_{1, 2}, a_{1, 3} \} = \{ 1, 5, 6 \}, {a2,1,a2,2,a2,3}={2,3,4}\{ a_{2, 1}, a_{2, 2}, a_{2, 3} \} = \{ 2, 3, 4 \}
  • {a1,1,a1,2,a1,3}={2,3,4}\{ a_{1, 1}, a_{1, 2}, a_{1, 3} \} = \{ 2, 3, 4 \}, {a2,1,a2,2,a2,3}={1,5,6}\{ a_{2, 1}, a_{2, 2}, a_{2, 3} \} = \{ 1, 5, 6 \}

[Sample #2]

See problem/problem2.in and problem/problem2.ans in the attachment.

In this sample, the nn of the five test cases are 3,5,8,12,173, 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 nn of the five test cases are 2,4,7,15,192, 4, 7, 15, 19, respectively.

[Constraints]

For all testdata, 1C51 \le C \le 5, 1n191 \le n \le 19. The length of the string SS is 3n3 n, and it consists only of 0,1,2,30, 1, 2, 3.

Test Point ID nn \le Special Property
11 None
22
33
44 55 A
55 77 None
66 1010
77 1313 A
88 1616 None
99 1818
1010 1919

Special property A: the string SS consists of all 00.

[Hint]

Please pay attention to the program’s memory usage.

Translated by ChatGPT 5