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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC target ("avx2")
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")

#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int) (x).size())
#define pb push_back
#define mp make_pair
#define int long long

using namespace std;
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; }
template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll mod = 1e9 + 7;
const ll base = 1e6 + 9;
const ll inf = 1e18;
const int MAX = 3e5 + 42;
const int LG = 20;

//random_device rd;
//mt19937 gen(rd());
//uniform_int_distribution<ll> dis(1, inf);


const int phi_minus_one = mod - 2;

class Mint {
    public:
        int x;

    public:
        void norm() {
            x %= mod;
            if(x < 0) x += mod;
        }
        Mint(int a, bool small) {
            x = a;
            if(x >= mod) x -= mod;
            if(x < 0) x += mod;
        }
        Mint() { x = 0; }
        Mint(ll a) {
            x = a % mod;
            if(x < 0) x += mod;
        }
        friend ostream &operator <<(ostream &out, const Mint &a) { out << a.x; return out; }
        friend istream &operator >>(istream &in, Mint &a) { in >> a.x; return in; }
        Mint operator +(const Mint &b) const {
            return Mint(x + b.x, 1);
        }
        Mint operator +(int a) {
            return Mint(x + a, 1);
        }
        Mint operator -(const Mint &b) const {
            return Mint(x - b.x, 1);
        }
        Mint operator -(int a) {
            return Mint(x - a, 1);
        }
        friend Mint operator -(Mint a) {
            return Mint(mod - a);
        }
        Mint operator *(const Mint &b) const {
             return Mint(1LL * x * b.x);
        }
        Mint operator *(int a) {
            return Mint(1LL * x * a);
        }
        Mint& operator +=(const Mint &b) {
            x += b.x;
            if(x >= mod) x -= mod;
            return *this;
        }
        Mint& operator +=(int a) {
            x += a;
            if(x >= mod) x -= mod;
            return *this;
        }
        Mint& operator -=(Mint b) {
            x += mod - b.x;
            if(x >= mod) x -= mod;
            return *this;
        }
        Mint& operator -=(int a) {
            x += mod - a;
            if(x >= mod) x -= mod;
            return *this;
        }
        Mint& operator *=(Mint b) {
            x = (ll) x * b.x % mod;
            return *this;
        }
        Mint& operator *=(int a) {
            x = (ll) x * a % mod;
            return *this;
        }
        Mint& operator ++() {
            if(++x == mod) x = 0;
            return *this;
        }
        Mint bpow(ll n) {
            Mint a(x);
            Mint ans(1);
            while(n) {
                if(n & 1) ans *= a;
                n >>= 1;
                a *= a;
            }
            return ans;
        }
        Mint inv() {
            return bpow(phi_minus_one);
        }
        Mint operator /(Mint b) {
            return b.inv() * x;
        }
        Mint operator /(int a) {
            return Mint(a, 1).inv() * x;
        }
        friend Mint operator -(int a, Mint b) {
            Mint res(b - a);
            res.x = mod - res.x;
            if(res.x == mod) res.x = 0;
            return res;
        }
        friend Mint operator +(int a, Mint b) {
            return Mint(b + a);
        }
        friend Mint operator *(int a, Mint b) {
            return Mint(b * a);
        }
        friend Mint operator /(int a, Mint b) {
            return Mint(a, 1) * b.inv();
        }
        Mint operator =(Mint b) {
            x = b.x;
            return b;
        }
        bool operator ==(int a) {
            return (x == a);
        }
        bool operator !=(int a) {
            return !(x == a);
        }
        friend bool operator ==(int a, Mint b) {
            return (b.x == a);
        }
        friend bool operator !=(int a, Mint b) {
            return b.x != a;
        }
};

struct fenwick {
    int N;
    vector<ll> f;

    fenwick(int n): N(n + 42), f(n + 42) {}

    void update(int p, int x) {
        p += 5;
        for(; p < N; p += p & -p) f[p] += x;
    }

    ll get(int p) {
        p += 5;
        ll ans = 0;
        for(; p; p -= p & -p) ans += f[p];
        return ans;
    }

    ll get(int l, int r) {
        return get(r) - get(l - 1);
    }
};

int cnt(vector<int> a) {
    int n = sz(a);
    int ans = 0;
    fenwick f(n + 1);
    for(int i = 0; i < n; i++) {
        ans += f.get(a[i], n);
        f.update(a[i], 1);
    }
    return ans;
}

vector<int> mult(vector<int> a, vector<int> b) {
    int n = sz(a);
    vector<int> ans(n);
    for(int i = 0; i < n; i++) ans[i] = a[b[i]];
    return ans;
}

void solve() {
//    for(int it = 1; it <= 100; it++) { ifstream cin("testy/" + to_string(it) + ".in");
    int n, k;
    cin >> n >> k;
    vector<vector<int>> a(k, vector<int>(n));
    for(auto &i : a) {
        for(auto &j : i) {
            cin >> j;
            j--;
        }
    }
    Mint sum = 0, total;
    if(k == 1) {
        auto p = a[0];
        vector<int> used(n);
        vector<int> siz;
        vector<vector<int>> cyc;
        vector<int> idx(n);
        vector<int> st(n);
        for(int i = 0; i < n; i++) {
            if(used[i]) continue;
            vector<int> curr;
            int j = i;
            while(!used[j]) {
                used[j] = 1;
                st[j] = sz(curr);
                curr.pb(j);
                idx[j] = sz(siz);
                j = p[j];
            }
            for(auto l : curr) {
                if(st[l] >= sz(curr)) st[l] -= sz(curr);
            }
            cyc.pb(curr);
            siz.pb(sz(curr));
        }
        int m = sz(siz);
        vector<vector<vector<int>>> precalc(m, vector<vector<int>>(m));
        for(int f = 0; f < m; f++) {
            for(int s = f + 1; s < m; s++) {
                int g = __gcd(siz[f], siz[s]);
                precalc[f][s].resize(g);
                for(int i = 0; i < siz[f]; i++) {
                    for(int j = 0; j < siz[s]; j++) {
                        if(cyc[f][i] > cyc[s][j]) continue;
                        int val = (i - j) % g; if(val < 0) val += g;
                        precalc[f][s][val]++;
                    }
                }
            }
        }
        Mint ans = 0;
        for(int f = 0; f < m; f++) {
            for(int s = f + 1; s < m; s++) {
                int g = __gcd(siz[f], siz[s]);
                int lcm = siz[f] * siz[s] / g;
                Mint adder = Mint(lcm).inv();
                for(int r1 = 0; r1 < siz[f]; r1++) {
                    for(int r2 = 0; r2 < siz[s]; r2++) {
                        if(cyc[f][r1] < cyc[s][r2]) continue;
                        int val = (r1 - r2) % g; if(val < 0) val += g;
                        ans += precalc[f][s][val] * adder;
                    }
                }
            }
        }
        ans *= 2;
        for(int f = 0; f < m; f++) {
            Mint adder = Mint(siz[f]).inv();
            vector<int> zero(siz[f]), one(siz[f]);
            for(int i = 0; i < siz[f]; i++) {
                for(int d = 1; d < siz[f]; d++) {
                    if(cyc[f][i] < cyc[f][(i + d) % siz[f]]) one[d]++;
                    else zero[d]++;
                }
            }
            for(int d = 1; d < siz[f]; d++) ans += adder * zero[d] * one[d];
        }
//        Mint exp; ifstream fin("testy/" + to_string(it) + ".out"); fin >> exp; cout << ans << " " << exp << endl; if(ans.x != exp.x) cout << "WRONG" << endl, exit(0);
        cout << ans << '\n';
    }
    else if(n > 13) {
        Mint ans = Mint(n) * (n - 1);
        ans = ans / 4;
        cout << ans << '\n';
    }
    else {
        set<vector<int>> dp = {a[0]};
        while(1) {
            auto ndp = dp;
            for(auto p : a) {
                for(auto i : dp) {
                    ndp.insert(mult(i, p));
                }
            }
            if(sz(dp) == sz(ndp)) break;
            dp = ndp;
        }
        total = sz(dp);
        sum = 0;
        for(auto p : dp) sum += cnt(p);
        cout << sum / total << '\n';
    }
//    }
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int ttt = 1;
//    cin >> ttt;
    while(ttt--) {
        solve();
    }
}