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
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r)for(int i=(l);i<=(r);++i)
#define REP(i,n)FOR(i,0,(n)-1)
#define ssize(x)int(x.size())
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
using vi = vector<int>;

constexpr int mod = 1'000'000'007;
int add(int a, int b) {
	a += b;
	return a >= mod ? a - mod : a;
}
int sub(int a, int b) {
	return add(a, mod - b);
}
int mul(int a, int b) {
	return int(a * LL(b) % mod);
}
int powi(int a, int b) {
	for(int ret = 1;; b /= 2) {
		if(b == 0)
			return ret;
		if(b & 1)
			ret = mul(ret, a);
		a = mul(a, a);
	}
}
int inv(int x) {
	return powi(x, mod - 2);
}

const int N = 3'007;
using B = bitset<N>;

int n, k;
B odw[N];
int t[N][N];

int sm, ct;
void dfs(int a, int b) {
	odw[a][b] = 1;
	++ct;
	sm += a < b;
	REP (g, k) {
		if (!odw[t[a][g]][t[b][g]])
			dfs(t[a][g], t[b][g]);
	}
}

void get(int &a) {
	int g = getchar_unlocked();
	while (g < '0' || '9' < g)
		g = getchar_unlocked();
	while ('0' <= g && g <= '9') {
		a = (a << 3) + (a << 1) + g - '0';
		g = getchar_unlocked();
	}
}

int main() {
	get(n);
	get(k);
	REP (i, k) {
		REP (j, n) {
			get(t[j][i]);
			--t[j][i];
		}
	}
	int ans = 0;
	REP (i, n) {
		REP (j, n) {
			if (odw[i][j])
				continue;
			sm = ct = 0;
			dfs(i, j);
			ans = add(ans, mul(mul(sm, sub(ct, sm)), inv(ct)));
		}
	}
	cout << ans << endl;
}