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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define SZ(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define st first
#define nd second
#define K long double
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
auto &operator<<(auto &o, pair<auto, auto> p) {
    return o << "(" << p.st << ", " << p.nd << ")";
}
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
    o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e;
    return o << "}";
}
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { ((cerr<<$<<"; "),...) << endl; }(x)

const int nax = 3005;
int a[nax][nax];
int n, k;

const int mod = 1e9 + 7;

ll pp(ll a, ll b){
    ll ans = 1;
    while(b){
        if(b & 1){
            ans *= a;
            ans %= mod;
        }
        a *= a;
        a %= mod;
        b /= 2;
    }
    return ans;
}

ll inv(ll a){
    // return 1.0 / a;
    return pp(a, mod - 2);
}

void add(int &x, int y){
    x += y;
    if(x >= mod) x -= mod;
}

int par[nax * nax];
int sz[nax * nax];

int f(int x){
    if(par[x] == x) return par[x];
    return par[x] = f(par[x]);
}

int u(int x, int y){
    x = f(x); y = f(y);
    if(x == y) return 0;
    if(sz[x] > sz[y]) swap(x, y);
    par[x] = y;
    sz[y] += sz[x];
    return 1;
}

vector<pii> kto[nax * nax];

void solve(){
    cin >> n >> k;
    rep(i, 1, k){
        rep(j, 1, n) cin >> a[i][j];
    }
    
    rep(i, 1, n * n){
        par[i] = i;
        sz[i] = 1;
    }

    rep(id, 1, k){
        int nowe = 0;
        rep(i, 1, n){
            int ti = a[id][i];
            int b1 = (i - 1) * n;
            int b2 = (ti - 1) * n;
            rep(j, 1, n){
                b1 += 1;
                if(i == j) continue;
                int other = b2 + a[id][j];
                if(par[b1] == par[other] || f(b1) == f(other)) continue;
                nowe += u(b1, b2 + a[id][j]);
            }
        }
        // if(nowe == 0) break;
    }
    
    rep(i, 1, n){
        rep(j, 1, n){
            if(i == j) continue;
            int who = (i - 1) * n + j;
            kto[f(who)].pb({i, j});
        }
    }

    int ans = 0;
    rep(i, 1, n * n){
        if(kto[i].empty()) continue;
        auto cyc = kto[i];
        int good = 0;
        int bad = 0;
        for(auto [x, y] : cyc){
            if(x < y) good += 1;
            else bad += 1;
        }
        // deb(cyc, good, bad);
        ll cur = 1LL * good * bad % mod;
        cur *= inv(good + bad);
        cur %= mod;
        add(ans, (int)cur);
    }
   
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;
    // cin >> tt;
    rep(te, 1, tt) solve();
    return 0;
}