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
#include<cstdio>
#include<algorithm>
#include<vector>
#define S 3007

using namespace std;
typedef long long ll;
ll mod = 1000000007;

ll pot(ll x, ll y){
    if(y == 0)
        return 1;
    if(y == 1)
        return x;
    if(y%2 == 0){
        ll p = pot(x,y/2);
        return (p*p)%mod;
    }else{
        return (pot(x,y-1) * x)%mod;
    }
}

int tmp[S];

vector<pair<int,int>> V[S][S];

bool vis[S][S];
int good, bad;

void DFS(int x, int y){
    if(x > y)
        good++;
    else
        bad++;
    vis[x][y] = 1;
    for(int i = 0; i < V[x][y].size(); i++){
        int x2 = V[x][y][i].first;
        int y2 = V[x][y][i].second;
        if(!vis[x2][y2]){
            DFS(x2,y2);
        }
    }
}

int main(void){
    int n,k;
    scanf("%d %d",&n,&k);
    for(int i = 1; i <= k; i++){
        for(int j = 1; j <= n; j++){
            scanf("%d", &tmp[j]);
        }
        for(int j = 1; j <= n; j++){
            for(int k = 1; k <= n; k++){
                //edge from (j,k) to (tmp[j], tmp[k])
                V[j][k].push_back({tmp[j], tmp[k]});
                V[tmp[j]][tmp[k]].push_back({j, k});
            }
        }
    }

    ll ans = 0;

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(!vis[i][j]){
                good = 0;
                bad = 0;
                DFS(i,j);

                ll p = pot(good+bad, mod-2);
                p *= good;
                p %= mod;
                p *= bad;
                p %= mod;
                ans += p;
            }
        }
    }
    ans %= mod;
    printf("%lld\n",ans);

    return 0;
}