Under construction

Still being built. Things may change

Eevee

Time limit5sMemory limit512 MB

Summary
Given k random stacks of n fragments, find the expected sum over all ranges of 2 or more stacks of interleavings with no k equal-stone fragments in a row.
Level

Hard9 of 10

Topics
Combinatorics, Probability, Dynamic programming, Math
Solved
No attempts yet

Problem

Eevee can evolve in various ways. For instance, it evolves when it comes close to one of the evolution stones.

There are nn different evolution stones, numbered from 1 to nn. Each stone is split into kk fragments. Eevee grouped all the fragments into kk stacks, numbered from 1 to kk. Each stack contains exactly nn fragments, each from a different stone, so each stack can be viewed as a permutation of the stone fragments.

Eevee performs the following operation k×nk \times n times. It picks a non-empty stack, removes its topmost fragment, and appends it to the end of a sequence that starts empty. Two ways of combining the stacks are different if at some step a different stack index is chosen. After the process, if the sequence contains kk neighboring fragments of the same stone, Eevee can evolve by accident. A way to combine the stacks is called good if no interval of length kk contains only fragments of the same stone.

Let f(i,j)f(i, j) be the number of good ways to combine the stacks with indices in the range [i,j][i, j]. For an interval of length ℓ\ell, we assume Eevee performs the operation only ℓ×n\ell \times n times, and no interval of length ℓ\ell may contain only fragments of the same stone.

Compute

(∑i=1k∑j=i+1kf(i,j)) mod (109+7)\left(\sum_{i=1}^{k}\sum_{j=i+1}^{k}f(i,j)\right) \bmod (10^9+7)

However, it turned out that Eevee shuffled each stack before the process. So each stack contains a random permutation of its fragments. Each input permutation is chosen uniformly from all n!n! permutations, independently of the others.

Input

The first line contains two integers kk and nn (2≤k,n≤3002 \le k, n \le 300), the number of stacks and the number of fragments in each stack. Each of the next kk lines contains nn integers. The jj-th number in the ii-th line is ai,ja_{i,j} (1≤ai,j≤n1 \le a_{i,j} \le n, and ai,j≠ai,la_{i,j} \ne a_{i,l} for j≠lj \ne l), the jj-th number from the top of the ii-th stack.

Output

Output the value of the sum described in the problem statement.

Hint

Consider calculating f(1,3)f(1, 3) in the first sample. Eevee has three stacks of the stone fragments:

One of the good ways to combine them is as follows:

The following one is not good, as it contains three 3s in a row:

In the first sample f(1,2)=8f(1, 2) = 8, f(1,3)=1446f(1, 3) = 1446, and f(2,3)=10f(2, 3) = 10. In the second sample f(1,2)=2f(1, 2) = 2, f(1,3)=66f(1, 3) = 66, f(1,4)=2328f(1, 4) = 2328, f(2,3)=2f(2, 3) = 2, f(2,4)=66f(2, 4) = 66, and f(3,4)=2f(3, 4) = 2.

Examples2

  1. Example 1

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

    Input
    4 2
    1 2
    2 1
    1 2
    2 1
    
    Expected output
    2466