#P8338. [AHOI2022] 排列
[AHOI2022] 排列
Problem Description
For a permutation of length and an integer , define the -th power of as
$$P^{(k)} = \left( p^{(k)}_1, p^{(k)}_2, \ldots, p^{(k)}_n \right),$$where the -th element of this permutation is
$$p^{(k)}_i = \begin{cases} i, & k = 0, \\ p^{(k - 1)}_{p_i}, & k > 0. \end{cases}$$It is easy to prove that any power of any permutation is still a permutation.
Define the cycle value of a permutation as the smallest positive integer such that .
Given a permutation of length , for integers , define as follows: if there exists such that , then ; otherwise, let the permutation be the permutation obtained by swapping the -th element and the -th element in , and set .
Compute the value of . The answer may be very large; you only need to output it modulo .
Input Format
This problem has multiple test cases. The first line contains an integer , indicating the number of test cases.
For each test case, the first line contains a positive integer , representing the length of the permutation. The next line contains integers , describing the given permutation.
Output Format
For each test case, output one line with one integer, representing the required answer modulo .
2
3
1 2 3
3
2 3 1
12
0
Hint
Sample Explanation #1
For the first test case, $f(1, 2) = f(2, 1) = f(2, 3) = f(3, 2) = f(1, 3) = f(3, 1) = 2$, and all other are .
For the second test case, all are .
Sample #2
See perm/perm2.in and perm/perm2.ans in the attachments.
In this sample, the first test case satisfies , the first four test cases satisfy , and all test cases satisfy .
Sample #3
See perm/perm3.in and perm/perm3.ans in the attachments.
In this sample, the first test case satisfies Special Property A, the second test case satisfies Special Property B, and the third test case satisfies Special Property C. The first four test cases satisfy , and the fifth test case satisfies .
For the exact definitions of the special properties, see the Constraints section.
Constraints
For of the testdata, , , .
| Test Point ID | Special Property | |
|---|---|---|
| A | ||
| None | ||
| B | ||
| C | ||
| None | ||
Special Property A: .
Special Property B: For any , there exists such that .
Special Property C: There exists a set of size at most such that for any , there exist such that .
Hint
The input size is large, so please use a relatively fast input method.
Translated by ChatGPT 5