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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#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,auto x)->decltype(x.end(),o);
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";}
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

struct FindUnion {
	vector<int> rep;
	int size(int x) { return -rep[find(x)]; }
	int find(int x) {
		return rep[x] < 0 ? x : rep[x] = find(rep[x]);
	}
	bool same_set(int a, int b) { return find(a) == find(b); }
	bool join(int a, int b) {
		a = find(a), b = find(b);
		if(a == b)
			return false;
		if(-rep[a] < -rep[b])
			swap(a, b);
		rep[a] += rep[b];
		rep[b] = a;
		return true;
	}
	FindUnion(int n) : rep(n, -1) {}
};

constexpr LL MOD = (1ll << 61) - 1;
LL reduce(LL x) { return x >= MOD ? x - MOD : x; }
LL mul(LL a, LL b) {
	const auto c = __int128(a) * b;
	return reduce(LL(c & MOD) + LL(c >> 61));
}

struct Fenwick {
	vector<int> s;
	Fenwick(int n) : s(n) {}
	void update(int pos, int val) {
		for(; pos < ssize(s); pos |= pos + 1)
			s[pos] += val;
	}
	int query(int pos) {
		int ret = 0;
		for(pos++; pos > 0; pos &= pos - 1)
			ret += s[pos - 1];
		return ret;
	}
	int query(int l, int r) {
		if (l > r) return 0;
		return query(r) - query(l - 1);
	}
};

int fastin() {
	int n = 0, c = getchar_unlocked();
	while(isspace(c))
		c = getchar_unlocked();
	while(isdigit(c)) {
		n = 10 * n + (c - '0');
		c = getchar_unlocked();
	}
	return n;
}
void fastout(int x) {
	if(x == 0) {
		putchar_unlocked('0');
		putchar_unlocked('\n');
		return;
	}
	static char t[10];
	int i = 0;
	while(x) {
		t[i++] = char('0' + (x % 10));
		x /= 10;
	}
	while(--i >= 0)
		putchar_unlocked(t[i]);
	putchar_unlocked('\n');
}

constexpr int mod = int(1e9) + 7;
int add(int a, int b) {
	a += b;
	return a >= mod ? a - mod : a;
}
void self_add(int& a, int b) {
	a = add(a, b);
}
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);
}
struct BinomCoeff {
	vector<int> fac, rev;
	BinomCoeff(int n) {
		fac = rev = vector(n + 1, 1);
		FOR(i, 1, n) fac[i] = mul(fac[i - 1], i);
		rev[n] = inv(fac[n]);
		for(int i = n; i > 0; --i)
			rev[i - 1] = mul(rev[i], i);
	}
	int operator()(int n, int k) {
		return mul(fac[n], mul(rev[n - k], rev[k]));
	}
};

int main() {
	int n, k;
	n = fastin();
	k = fastin();
	vector in(k, vector(n, 0));
	REP(i, k)
		REP(j, n) {
			in[i][j] = fastin();
			--in[i][j];
		}
	debug(n, k, in);

	auto get_cycles = [&](const vector<int>& v) {
		vector<vector<int>> ret;
		vector<bool> vis(n);
		REP(i, n) {
			if (vis[i])
				continue;
			int x = i;
			vector<int> h;
			while (not vis[x]) {
				vis[x] = true;
				h.emplace_back(x);
				x = v[x];
			}
			ret.emplace_back(h);
		}
		return ret;
	};
	auto init_hash = [&](const vector<int>& v) {
		auto cycles = get_cycles(v);
		bool rev = false;
		for (const auto& x : cycles) {
			if (ssize(x) >= 3 and x[1] > x.back()) {
				rev = true;
				break;
			}
		}
		if (rev) {
			for (auto& x : cycles)
				reverse(x.begin() + 1, x.end());
		}
		reverse(cycles.begin(), cycles.end());
		vector<int> ret;
		for (auto x : cycles)
			for (auto y : x)
				ret.emplace_back(y);
		debug(v, ret);
		return ret;
	};

	{
		set<vector<int>> seen;
		vector<int> id(n);
		iota(id.begin(), id.end(), 0);
		seen.emplace(init_hash(id));
		vector<vector<int>> temp;
		for (const auto& v : in) {
			debug(v, seen);
			const auto h = init_hash(v);
			if (seen.contains(h))
				continue;
			temp.emplace_back(v);
			seen.emplace(h);
		}
		swap(in, temp);
		k = ssize(in);
		debug(n, k, in);
	}

	auto finish = [](int ans) {
		cout << ans << '\n';
		exit(0);
	};

	if (k == 0) {
		finish(0);
	}

	if (k == 1) {
		int ans = 0;
		const auto v = in[0];
		const auto cycles = get_cycles(v);
		debug(cycles);
		for (auto a : cycles) {
			for (auto b : cycles) {
				const int sa = ssize(a);
				const int sb = ssize(b);
				const int step = gcd(sa, sb);
				const int len = sa * sb / step;
				debug(sa, sb, len, step, a, b);

				vector cnt(step, vector (step, 0));
				REP(i, step) {
					vector<int> values;
					for (int l = i; l < sa; l += step) {
						values.emplace_back(a[l]);
					}
					sort(values.begin(), values.end());
					REP(l, sb) {
						cnt[i][l % step] += int(lower_bound(values.begin(), values.end(), b[l]) - values.begin());
					}
				}
				debug(a, b, cnt);

				vector sum(step, 0);
				const int complement = len / step;
				REP(i, step) {
					REP(j, step) {
						self_add(sum[(i - j + step) % step], complement - cnt[i][j]);
					}
				}

				int cur = 0;
				REP(i, step) {
					REP(j, step) {
						self_add(cur, mul(cnt[i][j], sum[(i - j + step) % step]));
					}
				}

				self_add(ans, mul(cur, inv(len)));
			}
		}
		finish(ans);
	}

	auto formula = [&]() {
		finish(mul(n, mul(n - 1, inv(4))));
	};

	{
		assert(n >= 2);
		auto get_id = [&](int i, int j) {
			return i * n + j;
		};
		FindUnion fu(n * n);
		for (auto p : in) {
			REP(i, n) {
				REP(j, n) {
					fu.join(get_id(i, j), get_id(p[i], p[j]));
				}
			}
			if (fu.size(get_id(0, 1)) == n * (n - 1)) {
				formula();
			}
		}
	}

	unordered_set<LL> seen;

	using SI = int16_t;
	using V = vector<SI>;
	using ULL = uint64_t;

	auto inversions_mask = [&](ULL v) {
		int ret = 0;
		auto val = v;
		REP(i, n) {
			const auto x = val & 15;
			auto c = v;
			REP(j, i) {
				ret += (c & 15) > x;
				c >>= 4;
			}
			val >>= 4;
		}
		return ret;
	};
	auto inversions_fenwick = [&](const V& v) {
		int ret = 0;
		Fenwick tree(n);
		for (int i = n - 1; i >= 0; --i) {
			ret += tree.query(v[i]);
			tree.update(v[i], 1);
		}
		return ret;
	};
	auto compose_mask = [&](ULL a, const V& b) {
		ULL ret = 0;
		REP(i, n) {
			ret ^= ULL(1ull << ULL(4 * i)) * ULL(b[a & 15]);
			a >>= 4;
		}
		return ret;
	};
	auto compose = [&](const V& a, const V& b) {
		V v(n);
		REP(i, n)
			v[i] = a[b[i]];
		return v;
	};

	mt19937 rng(0);
	const LL base = rng();
	debug(base);

	auto hash = [&](const V& v) -> LL {
		LL ret = 0;
		for (auto x : v) {
			ret = reduce(mul(ret, base) + x + 1);
		}
		return ret;
	};

	vector<V> IN(k);
	REP(i, k) {
		IN[i] = {in[i].begin(), in[i].end()};
	}

	auto mask_brute = [&]() {
		int ans = 0, cnt = 0;
		unordered_set<ULL> visited;
		queue<ULL> q;
		ULL id = 0;
		REP(i, n)
			id ^= ULL(1ull << ULL(4 * i)) * ULL(i);
		q.emplace(id);
		visited.emplace(id);
		while (ssize(q)) {
			auto v = q.front();
			debug(v);
			q.pop();
			++cnt;
			self_add(ans, inversions_mask(v));
			for (auto p : IN) {
				const auto next = compose_mask(v, p);
				if (not visited.contains(next)) {
					visited.emplace(next);
					q.emplace(next);
				}
			}
		}
		ans = mul(ans, inv(cnt));
		finish(ans);
	};

	auto vector_brute = [&]() {
		int ans = 0, cnt = 0;
		unordered_set<LL> visited;
		queue<V> q;
		V id(n);
		iota(id.begin(), id.end(), 0);
		q.emplace(id);
		visited.emplace(hash(id));
		while (ssize(q)) {
			auto v = q.front();
			debug(v);
			q.pop();
			++cnt;
			self_add(ans, inversions_fenwick(v));
			for (auto p : IN) {
				const auto next = compose(v, p);
				const auto h = hash(next);
				if (not visited.contains(h)) {
					visited.emplace(h);
					q.emplace(next);
				}
			}
		}
		ans = mul(ans, inv(cnt));
		finish(ans);
	};

	if (n <= 16) {
		mask_brute();
	}
	else {
		vector_brute();
	}
}