This page is still under construction.

Parts of this page are still being built. What you see may change.

Bunch of Paper

Time limit2sMemory limit512 MB

Summary
Count non-decreasing sequences that pick one value from each of N sorted lists of K integers, modulo 1e9+7.
Level

Medium6 of 10

Topics
Dynamic programming, Sorting, Binary search, Combinatorics
Solved
No attempts yet

Problem

There are NN sheets of paper, numbered with the consecutive integers from 11 to NN. Each sheet has KK integers written on it, so the ii-th sheet contains vi,1,vi,2,…,vi,Kv_{i,1}, v_{i,2}, \ldots, v_{i,K}.

Choose one integer from each sheet to form the sequence aia_i, where the integer chosen from the ii-th sheet becomes aia_i. There are KNK^N ways to form such a sequence. How many of them are non-decreasing? A sequence is non-decreasing if ai≤ai+1a_i \le a_{i+1} for every 1≤i≤N−11 \le i \le N-1.

The answer may be large, so print it modulo 109+710^9 + 7.

Input

The first line contains two integers NN and KK (1≤N≤1001 \le N \le 100, 1≤K≤1041 \le K \le 10^4). The ii-th of the following NN lines contains KK integers vi,1,vi,2,…,vi,Kv_{i,1}, v_{i,2}, \ldots, v_{i,K} (1≤vi,1<vi,2<…<vi,K≤1091 \le v_{i,1} < v_{i,2} < \ldots < v_{i,K} \le 10^9).

Output

Print the number of non-decreasing sequences modulo 109+710^9 + 7.

Examples2

  1. Example 1

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

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