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
// Author: Kajetan Ramsza
#include "bits/stdc++.h"
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; }

void dbg_out() { cerr<<'\n'; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); }

#ifdef DEBUG
#define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 
#endif


vi inverse(const vi &perm) {
	vi res(sz(perm));
	rep(i,0,sz(perm)) res[perm[i]] = i;
	return res;
}

vi operator*(const vi &a, const vi &b) {
  	vi res(sz(a));
  	rep(i,0,sz(a)) res[i] = b[a[i]];
  	return res;
}

struct PermutationGroup {
  	int n, m;
  	vector<vi> lookup;
  	vector<vector<vi>> buckets, ibuckets;

  	int val(vi p, bool add_to_group = 1){
    	n = sz(buckets);
    	rep(i,0,n) {
      		int res = lookup[i][p[i]];
	      	if(res == -1) {
		        if(add_to_group) {
		          	buckets[i].push_back(p);
		          	ibuckets[i].push_back(inverse(p));
		          	lookup[i][p[i]] = sz(buckets[i]) - 1;
		        }
		        return i;
	      	}
      		p = p * ibuckets[i][res];
    	}
    	return -1;
  	}	

  	bool in_group(vi perm) { 
  		return val(perm, false) == -1; 
  	}

  	PermutationGroup(vector<vi> &generators, int _n) : n(_n), m(sz(generators)) {
	    lookup.resize(n);
	    buckets.resize(n);
	    ibuckets.resize(n);
	    rep(i,0,n) {
	      	lookup[i].resize(n);
	      	fill(all(lookup[i]), -1);
	    }
	    vi id(n); iota(all(id), 0);
	    rep(i,0,n) {
	      	buckets[i].push_back(id);
	      	ibuckets[i].push_back(id);
	      	lookup[i][i] = 0;
	    }
	    rep(i,0,m) val(generators[i]);
	    queue<pair<pii,pii>> q;
	    rep(i,0,n) rep(j,i,n)
	        rep(k,0,sz(buckets[i])) rep(l,0,sz(buckets[j]))
	            q.push({{i, k}, {j, l}});
	        
	    while(!q.empty()) {
	      	auto a = q.front().first;
	      	auto b = q.front().second;
	      	q.pop();
	      	int res = val(buckets[a.first][a.second] * buckets[b.first][b.second]);
	      	if (res == -1) continue;
	      	pii curr(res, sz(buckets[res]) - 1);
	      	rep(i,0,n) rep(j,0,sz(buckets[i])) {
	          	if (i <= res) q.push({{i, j}, curr});
	          	if (res <= i) q.push({curr, {i, j}});
	        }
	    }
	 }
};

#include <bits/extc++.h> /** keep-include */
using namespace __gnu_pbds;

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
    tree_order_statistics_node_update>;

const ll mod = 1000000007;

ll modpow(ll b, ll e) {
	ll ans = 1;
	for (; e; b = b * b % mod, e /= 2)
		if (e & 1) ans = ans * b % mod;
	return ans;
}

ll calc_inv_in_array(const vi &vec) {
    ordered_set<int> s;
	ll res = 0;
    rep(i,0,sz(vec)) { 
    	res += ll(i) - (ll)s.order_of_key(vec[i]);
        s.insert(vec[i]); 
    }
    return res; 
} 

int n, k;

namespace SubtaskK1 {
	vi perm;

	ll calc_inv_in_cycle(const vi &cycle) {
		int n = sz(cycle);
		vector<pii> sorted(n);
		rep(i,0,n) sorted[i] = {cycle[i], i};
		sort(all(sorted));
		vi vals(n);
		ll res = 0;
		rep(x,1,n+1) {
			rep(i,0,n) vals[i] = cycle[(sorted[i].second + x) % n];
			res += calc_inv_in_array(vals);
		}
		dbg(cycle, res);
		return res;
	}

	ll calc_lcm(ll a, ll b) {
		return (a * b / __gcd(a,b)) % mod;
	}

	ll index(ll i, ll j, ll gcd) {
		return ((i%gcd)-(j%gcd)+gcd) % gcd;
	}

	ll calc_inv_between_cycles(const vi &a, const vi &b) {
		ll gcd = __gcd(sz(a), sz(b));
		vector<ll> num(gcd, 0);
		rep(i,0,sz(a)) rep(j,0,sz(b))
			num[index(i,j,gcd)] += a[i] > b[j];
		// dbg(num);
		ll lcm = calc_lcm(sz(a), sz(b));
		ll res = 0;
		rep(i,0,sz(a)) rep(j,0,sz(b)) {
			res = (res  + (a[i] < b[j] ? num[index(i,j,gcd)] : lcm - num[index(i,j,gcd)])) % mod;
			// dbg(i,j, a[i],b[j], res);
		}
		dbg(a, b, res);
		return res;
	}

	vector<vi> cycle;
	vector<bool> vis;

	void dfs(int v) {
		cycle.back().push_back(v);
		vis[v] = true;
		if(!vis[perm[v]]) dfs(perm[v]);
	}

	void solve() {
		perm.resize(n);
		vis.assign(n, 0);
		rep(i,0,n) {
			cin>>perm[i];
			perm[i]--;
		}
		rep(i,0,n) {
			if(!vis[i]) {
				cycle.push_back({});
				dfs(i);
			}
		}
		dbg(cycle);

		ll inv = 0;
		rep(i,0,sz(cycle)) {
			inv = (inv + calc_inv_in_cycle(cycle[i]) * modpow(sz(cycle[i]), mod-2)) % mod;
		}

		rep(i,0,sz(cycle)) {
			rep(j,i+1,sz(cycle)) {
				inv = (inv + calc_inv_between_cycles(cycle[i], cycle[j]) * 
					modpow(calc_lcm(sz(cycle[i]), sz(cycle[j])), mod-2)) % mod;
			}
		}

		cout<<inv<<'\n';
	}
}

vector<vi> find_permutations() {
  	ios_base::sync_with_stdio(0);
  	cin.tie(0);

  	vector<vi> p(k, vi(n));
  	rep(i,0,k) rep(j,0,n) {
  		cin>>p[i][j];
  		p[i][j]--;
  	}
  	PermutationGroup g(p, n);
  	vi perm(n);
  	iota(all(perm), 0);
  	vector<vi> res;
  	do {
    	if(g.in_group(perm)) res.push_back(perm);
  	} while(next_permutation(all(perm)));
  	return res;
}


int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin>>n>>k;
	if(k == 1) {
		SubtaskK1::solve();
		return 0;
	}
	auto permutations = find_permutations();
	ll sum = 0;
	for(auto v : permutations)
		sum += calc_inv_in_array(v);
	ll res = sum * modpow(ll(sz(permutations)), mod-2) % mod;
	cout<<res<<'\n';
}