1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <ios>
#include <ranges>
#include <numeric>
#include <functional>
#include <cassert>
#include <vector>
#define REP(i, n) for(int i=0; i<(n); ++i)
#define SREP(n) REP(_ ## n, n)
#define FOR(i, p, n) for(int i=(p); i<=(n); ++i)
#define RFOR(i, p, n) for(int i=(p); i>=(n); --i)
#define pb push_back
#define eb emplace_back
#define C const
#define V std::vector
#define F std::function
#define R std::ranges
#define RV std::ranges::views
#define sz(A) int(A.size())
#define all(A) A.begin(), A.end()
#define rall(A) A.rbegin(), A.rend()
typedef long long ll;
typedef long double ld;
typedef V <int> vi;
typedef V <vi> vvi;
typedef V <bool> vb;
typedef V <char> vc;
typedef V <ll> vll;
typedef const int ci;
typedef const short cs;
typedef const ll cll;
int I(){
    int z;
    while ((z=getchar_unlocked())<'0'||z>'9');
    int r=z-'0';
    while ((z=getchar_unlocked())>='0'&&z<='9')
        r=r*10+z-'0';
    return r;
}
ci MOD=1e9+7;
ll poteg(ll a, ll w){
    ll r=1;
    for (; w; w>>=1,a=a*a%MOD)
        if (w&1)
            r=r*a%MOD;
    return r;
}
ci n=I(),m=I(),cap=3000;
//vvi v(n, vi(m));
//vvi grp(n, vi(n));
short v[cap][cap];
bool grp[cap][cap];
int ile,inv;
void bfs(cs pp, cs kp){
    struct idk{
        short p,k;
    };
    static idk kol[cap*cap];
    int ptr=0;
    kol[0]={pp, kp};
    grp[pp][kp]=1;
    while (ptr>=0){
        C auto [p, k]=kol[ptr];
        --ptr;
        ++ile,inv+=p>k;
        REP(i, m){
            cs np=v[p][i],nk=v[k][i];
            if (!grp[np][nk])
                grp[np][nk]=1,kol[++ptr]={np, nk};
        }
    }
}
int main(){
    REP(i, m)
        REP(j, n)
            v[j][i]=short(I()-1);
    ll w=0;
    REP(b, n)
        REP(a, b)
            if (!grp[a][b]){
                ile=0,inv=0;
                bfs(short(a), short(b));
                w=(w+ll(inv)*poteg(ile, MOD-2)%MOD*(ile-inv)%MOD)%MOD;
            }
    //fprintf(stderr, "%lld %lld\n", w, num);
    printf("%lld\n", w);
}