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
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, b, e) for(int i = (b); i < (e); i++)
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

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 << "}"; }
#ifdef LOCAL
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \
					((cerr << $ << "; "),...) << endl; }(x)
#else
#define deb(...)
#endif

const int mod = 1'000'000'007;

int inv(int base) {
	int res = 1;
	for(int exp = mod - 2; exp; exp >>= 1) {
		if(exp & 1) res = 1ll * res * base % mod;
		base = 1ll * base * base % mod;
	}
	return res;
}

const int N = 3010;

int perm[N];
int col[N][N], good[N * N];
vi kto[N * N];

void sc(int &num) {
	char c;
	num = 0;
	while(c = getchar_unlocked(), c < 33);
	for(; c > 47; c = getchar_unlocked()) num = num * 10 + c - '0';
}

void solve() {
	int n, k;
	sc(n), sc(k);
	FOR(i, 0, n) FOR(j, 0, n) {
		int p = i * n + j;
		col[i][j] = p;
		good[p] = i > j;
		kto[p] = {p};
	}
	FOR(kk, 0, k) {
		FOR(i, 0, n) {
			sc(perm[i]);
			perm[i]--;
		}
		FOR(i, 0, n) {
			int q = perm[i];
			FOR(j, 0, n) {
				if(col[i][j] != col[q][perm[j]]) {
					int a = col[i][j], b = col[q][perm[j]];
					if(sz(kto[a]) < sz(kto[b])) swap(a, b);
					good[a] += good[b];
					for(int &x: kto[b]) {
						col[x / n][x % n] = a;
						kto[a].pb(x);
					}
					kto[b] = {};
				}
			}
		}
	}
	int ans = 0;
	FOR(p, 0, n * n) if(p == col[p / n][p % n]) {
		ans = (ans + 1ll * good[p] * (sz(kto[p]) - good[p]) % mod * inv(sz(kto[p]))) % mod;
	}
	cout << ans << '\n';
}

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