#arc154e. [arc154_e]Reverse and Inversion
[arc154_e]Reverse and Inversion
Problem Statement
For a permutation of , let be the following value:
the sum of over all pairs of integers such that and .
You are given a permutation of .
Let us repeat the following operation times.
- Choose a pair of integers such that . Reverse . Formally, replace the values of with simultaneously.
There are ways to repeat the operation. Assume that we have computed for all those ways.
Find the sum of these values, modulo .
Constraints
- is a permutation of .
Input
The input is given from Standard Input in the following format:
Output
Print a single line containing the answer.
Sample Input 1
2 1
1 2
Sample Output 1
1
There are three ways to perform the operation, as follows.
- Choose , making , where .
- Choose , making , where .
- Choose , making , where .
Thus, the answer is .
Sample Input 2
3 2
3 2 1
Sample Output 2
90
Sample Input 3
10 2023
5 8 1 9 3 10 4 7 2 6
Sample Output 3
543960046