Under construction

Still being built. Things may change

Totoro

Time limit1sMemory limit1024 MB

Summary
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 KK permutations p1,…,pKp_1, \dots, p_K of length NN.

Let SS be the set of all permutations that can be obtained by composing finitely many of the permutations pip_i. The composition of permutations π\pi and τ\tau has the element π(τ(i))\pi(\tau(i)) at position ii. For example, for π=(1,3,2)\pi = (1, 3, 2) and τ=(2,3,1)\tau = (2, 3, 1), π∘τ=(3,2,1)\pi \circ \tau = (3, 2, 1). The same permutation may be composed multiple times.

Since SS is contained in the set of all permutations of length NN, SS is finite.

The number of inversions I(π)I(\pi) of a permutation π\pi is the number of pairs 1≤i<j≤N1 \le i < j \le N with π(i)>π(j)\pi(i) > \pi(j).

Find 1∣S∣∑π∈SI(π)\frac{1}{|S|}\sum_{\pi \in S} I(\pi). This value can be written as an irreducible fraction AB\frac{A}{B}, where BB is not divisible by 109+710^9 + 7. Print XX with 0≤X<109+70 \le X < 10^9 + 7 and X⋅B≡A(mod109+7)X \cdot B \equiv A \pmod{10^9 + 7}.

Input

The first line contains the natural numbers KK and NN.

Each of the next KK lines contains the permutation pip_i as NN distinct integers from 11 to NN, separated by spaces.

Output

Print the answer modulo 109+710^9 + 7 on a single line.

Hint

Explanation of the first test case: S={(2,3,1),(3,1,2),(1,2,3)}S = \{(2, 3, 1), (3, 1, 2), (1, 2, 3)\}. The first permutation has two inversions, the second also has two, and the identity has none. The average is 43\frac{4}{3}, which matches the printed value modulo 109+710^9 + 7.

Explanation of the second test case: one can check that SS equals the set of all permutations of {1,2,3,4,5}\{1, 2, 3, 4, 5\}, so composing the given permutations yields every other permutation.

Explanation of the third test case: here ∣S∣=20|S| = 20, and the answer is the fraction 14910\frac{149}{10} modulo 109+710^9 + 7.

Examples3

  1. Example 1

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

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

    Input
    1 9
    3 4 5 6 7 8 1 9 2
    
    Expected output
    300000017