Permutation Group
Time limit15sMemory limit1024 MB
Given k permutations, find the average number of inversions over all permutations in the group they generate, modulo 1e9+7.
- Level
Hard9 of 10
- Topics
- Combinatorics, Math
- Solved
- No attempts yet
Problem
This problem deals with permutations of elements. Each permutation is a sequence of distinct natural numbers from 1 to .
The composition of permutations and is . An inversion in a permutation is any index pair with and .
Bytek is a big fan of permutations and has favorite ones. He began writing on paper every permutation that can be obtained by composing his favorite permutations in any order. Some of them may be used more than once. He made sure to write each permutation at most once.
He soon ran out of paper. Then he wondered: if he wrote all reachable permutations, what would be the average number of inversions in them?
Write a program that computes this value. Print it modulo , as described in the Output section.
Input
The first line contains two integers and (), the length of the permutations and the number of Bytek's favorite permutations.
Each of the next lines contains one favorite permutation. The -th line holds (), and all numbers in a line are distinct.
Output
Print one integer: the average number of inversions over all permutations Bytek could write, modulo .
Formally, let the answer be with and . Print , where is the unique integer from to that satisfies .
It can be proven that for every valid test, the reduced denominator of the answer is not divisible by .
Hint
Explanation of the example: in the first case, Bytek writes with zero inversions, with two inversions, and with two inversions. The average is . Since , the answer is .
In the second case, Bytek writes all permutations of 5 elements. Their average number of inversions is exactly 5.