#P8338. [AHOI2022] 排列

    ID: 9417 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学暴力数据结构各省省选平衡树2022安徽O2优化优先队列素数判断,质数,筛法

[AHOI2022] 排列

Problem Description

For a permutation P=(p1,p2,,pn)P = (p_1, p_2, \ldots, p_n) of length nn and an integer k0k \ge 0, define the kk-th power of PP as

$$P^{(k)} = \left( p^{(k)}_1, p^{(k)}_2, \ldots, p^{(k)}_n \right),$$

where the ii-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 v(P)v(P) of a permutation PP as the smallest positive integer kk such that P(k+1)=PP^{(k + 1)} = P.

Given a permutation A=(a1,a2,,an)A = (a_1, a_2, \ldots, a_n) of length nn, for integers 1i,jn1 \le i, j \le n, define f(i,j)f(i, j) as follows: if there exists k0k \ge 0 such that ai(k)=ja^{(k)}_i = j, then f(i,j)=0f(i, j) = 0; otherwise, let the permutation AijA_{i j} be the permutation obtained by swapping the ii-th element aia_i and the jj-th element aja_j in AA, and set f(i,j)=v(Aij)f(i, j) = v(A_{i j}).

Compute the value of i=1nj=1nf(i,j)\sum_{i = 1}^{n} \sum_{j = 1}^{n} f(i, j). The answer may be very large; you only need to output it modulo (109+7)({10}^9 + 7).

Input Format

This problem has multiple test cases. The first line contains an integer TT, indicating the number of test cases.

For each test case, the first line contains a positive integer nn, representing the length of the permutation. The next line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n, describing the given permutation.

Output Format

For each test case, output one line with one integer, representing the required answer modulo (109+7)({10}^9 + 7).

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 f(i,j)f(i, j) are 00.

For the second test case, all f(i,j)f(i, j) are 00.

Sample #2

See perm/perm2.in and perm/perm2.ans in the attachments.

In this sample, the first test case satisfies n35n \le 35, the first four test cases satisfy n200n \le 200, and all test cases satisfy n2500n \le 2500.

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 n105n \le {10}^5, and the fifth test case satisfies n5×105n \le 5 \times {10}^5.

For the exact definitions of the special properties, see the Constraints section.

Constraints

For 100%100\% of the testdata, 1T51 \le T \le 5, 1n5×1051 \le n \le 5 \times {10}^5, 1ain1 \le a_i \le n.

Test Point ID nn \le Special Property
121 \sim 2 105{10}^5 A
33 3535 None
44 200200
55 25002500
66 105{10}^5 B
77 C
88 None
9109 \sim 10 5×1055 \times {10}^5

Special Property A: ai=(imodn)+1a_i = (i \bmod n) + 1.
Special Property B: For any 1in1 \le i \le n, there exists 1k201 \le k \le 20 such that ai(k)=ia^{(k)}_i = i.
Special Property C: There exists a set SS of size at most 1010 such that for any 1in1 \le i \le n, there exist xS,k0x \in S, k \ge 0 such that ax(k)=ia^{(k)}_x = i.

Hint

The input size is large, so please use a relatively fast input method.

Translated by ChatGPT 5