Totoro
Time limit1sMemory limit1024 MB
Given K permutations of length N, find the average inversion count over all permutations generated by composing them, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Union-find, Combinatorics, Math
- Solved
- No attempts yet
Problem
You are given permutations of length .
Let be the set of all permutations that can be obtained by composing finitely many of the permutations . The composition of permutations and has the element at position . For example, for and , . The same permutation may be composed multiple times.
Since is contained in the set of all permutations of length , is finite.
The number of inversions of a permutation is the number of pairs with .
Find . This value can be written as an irreducible fraction , where is not divisible by . Print with and .
Input
The first line contains the natural numbers and .
Each of the next lines contains the permutation as distinct integers from to , separated by spaces.
Output
Print the answer modulo on a single line.
Hint
Explanation of the first test case: . The first permutation has two inversions, the second also has two, and the identity has none. The average is , which matches the printed value modulo .
Explanation of the second test case: one can check that equals the set of all permutations of , so composing the given permutations yields every other permutation.
Explanation of the third test case: here , and the answer is the fraction modulo .