#P16400. [ECUSTPC 2026 Spring] 朝复夕
[ECUSTPC 2026 Spring] 朝复夕
Background
:::epigraph Morning again, evening; morning and evening again. :::
Problem Description
This problem is not related to Problem G “Morning Review”.
Little T plans to prepare some classic problems……
Given a permutation of length , Little T will perform one operation :
- Choose a pair of indices , and swap and in .
For all different valid operations , Little T needs to compute the sum of the orders of the permutations represented by the resulting permutations after the swap.
Since the answer may be very large, Little T needs to compute the answer modulo .
Please help Little T solve this problem.
The hints of the problem include some information about permutations and arrangements.
Input Format
The first line contains an integer , indicating the number of testdata.
For each testdata, the first line contains an integer , indicating the length of the permutation.
The next line contains integers , describing the given permutation.
It is guaranteed that over all testdata, and in each testdata the given sequence is a valid permutation.
Output Format
For each testdata, output one integer per line: for all different valid operations , the sum of the orders of the permutations represented by the permutation after swapping, modulo .
2
3
2 3 1
4
2 1 4 3
6
20
Hint
Explanation of Sample 1
For the -st testdata, there are different ways to swap:
- Swap and . The permutation becomes . Note that , because , , . Therefore, the order is .
- Swap and . The permutation becomes . Note that , and the order is .
- Swap and . The permutation becomes . Note that , and the order is .
Therefore, the answer is .
Hint
A permutation of to is a sequence of length in which every integer from to appears exactly once. The length of this permutation is also .
Let . A permutation (bijection) of to means a bijection from to .
A permutation of length , , can represent a permutation (bijection) , i.e., .
For example, in Sample 1, represents the permutation , where
The identity permutation of length , $\mathrm{id}_n: \{1, 2, \dots, n\} \to \{1, 2, \dots, n\}$, is defined as . For example, the identity permutation of length , , has:
$$\mathrm{id}_4(1) = 1,\ \mathrm{id}_4(2) = 2,\ \mathrm{id}_4(3) = 3,\ \mathrm{id}_4(4) = 4.$$The composition of two permutations and , written as , means the permutation .
The power of a permutation of length (where is a non-negative integer) is defined as
$$p^m = \begin{cases} p \circ p^{m-1}, & m \ge 1; \\ \mathrm{id}_n, & m = 0. \end{cases}$$The order of a permutation of length is defined as the smallest positive integer such that .
Translated by ChatGPT 5