Under construction

Still being built. Things may change

Permutation Group

Time limit15sMemory limit1024 MB

Summary
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 nn elements. Each permutation is a sequence of nn distinct natural numbers from 1 to nn.

The composition of permutations a1,a2,…,ana_1, a_2, \ldots, a_n and b1,b2,…,bnb_1, b_2, \ldots, b_n is ab=(ab1,ab2,…,abn)ab = (a_{b_1}, a_{b_2}, \ldots, a_{b_n}). An inversion in a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n is any index pair (i,j)(i, j) with i<ji < j and pi>pjp_i > p_j.

Bytek is a big fan of permutations and has kk 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 109+710^9 + 7, as described in the Output section.

Input

The first line contains two integers nn and kk (1≤n,k≤30001 \le n, k \le 3000), the length of the permutations and the number of Bytek's favorite permutations.

Each of the next kk lines contains one favorite permutation. The ii-th line holds ai,1,ai,2,…,ai,na_{i,1}, a_{i,2}, \ldots, a_{i,n} (1≤ai,j≤n1 \le a_{i,j} \le n), and all nn numbers in a line are distinct.

Output

Print one integer: the average number of inversions over all permutations Bytek could write, modulo 109+710^9 + 7.

Formally, let the answer be p/qp/q with q≠0q \neq 0 and gcd⁡(p,q)=1\gcd(p, q) = 1. Print p×q−1 mod (109+7)p \times q^{-1} \bmod (10^9 + 7), where q−1q^{-1} is the unique integer from 11 to 109+610^9 + 6 that satisfies qq−1≡1(mod109+7)q q^{-1} \equiv 1 \pmod{10^9 + 7}.

It can be proven that for every valid test, the reduced denominator of the answer is not divisible by 109+710^9 + 7.

Hint

Explanation of the example: in the first case, Bytek writes {1,2,3}\{1, 2, 3\} with zero inversions, {2,3,1}\{2, 3, 1\} with two inversions, and {3,1,2}\{3, 1, 2\} with two inversions. The average is 4/34/3. Since 3−1≡333333336(mod109+7)3^{-1} \equiv 333333336 \pmod{10^9 + 7}, the answer is 333333336×4≡333333337(mod109+7)333333336 \times 4 \equiv 333333337 \pmod{10^9 + 7}.

In the second case, Bytek writes all permutations of 5 elements. Their average number of inversions is exactly 5.

Examples2

  1. Example 1

    Input
    3 1
    2 3 1
    
    Expected output
    333333337
    
  2. Example 2

    Input
    5 2
    2 1 3 4 5
    2 3 4 5 1
    
    Expected output
    5