#P13795. [SWERC 2023] Flag performance
[SWERC 2023] Flag performance
题目描述
:::align{center}
:::
You are in charge of a team of sorted gymnastics. This new discipline involves teams of members. Each team member dresses with a different colour (a number from to ) and holds a coloured flag. Flags have unique colours, also numbered from to . A performance consists of exactly steps. At each step, two members exchange their flags. You are free to choose the initial configuration of the flags. The only constraint is that, at the end of the performance, each participant must hold the flag corresponding to the colour of his outfit.
Being the team captain, you would like the performance to be as unpredictable as possible. You consider possible initial configurations of flags among the team members, and wonder: in how many ways can the team perform the task for each of these initial configurations?
For each of the given initial configurations, compute the number of possible ways to do the performance. As the answers may be very large, return them modulo the prime number .
输入格式
Each line consists of space-separated integers. The first input line contains the numbers , , and . Then follow lines. The such line consists of distinct space-separated integers , representing the initial configuration of flags among team members. Here, is colour number of the flag initially in the hands of the team member whose outfit colour is .
Limits
- ;
- .
- .
输出格式
The output should contain lines. The such line should consist of a single number: the number of possible sequences of exchanges that start from the configuration and satisfy the constraints listed above, modulo .
4 2 1
4 1 2 3
0
4 3 1
4 1 2 3
16
6 15 10
5 6 1 2 4 3
2 4 1 6 5 3
4 1 3 6 5 2
1 3 2 4 5 6
4 5 6 1 2 3
1 2 5 3 6 4
6 4 2 3 1 5
3 6 4 1 2 5
4 5 1 2 6 3
6 1 4 3 2 5
310571736
0
745108126
996135367
597596468
745108126
0
0
310571736
0
提示
Sample Explanation 1
It is impossible to sort the flags with two exchanges.