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
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<(int)b;++i)
#define FORD(i,a,b) for(int i=a;i>=(int)b;--i)
#define PB push_back
#define EB emplace_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;
using ld = long double;

template <class T>
istream& operator>> (istream& is, vector<T>& vec){
    FOR(i,0,vec.size()) is >> vec[i];
    return is;
}
template <class T>
ostream& operator<< (ostream& os, const vector<T>& vec){
    for(auto& t : vec) os << t << " ";
    return os;
}
template<class T, class U>
ostream& operator<< (ostream& os, const pair<T, U>& p){
    os << p.FI << " " << p.SE;
    return os;
}
template<class T, class U>
istream& operator>> (istream& is, pair<T, U>& p){
    is >> p.FI >> p.SE;
    return is;
}

const ll MOD = 1000000007;

// https://github.com/ShahjalalShohag/code-library/blob/main/Miscellaneous/Schreier%E2%80%93Sims%20algorithm.cpp
// time complexity : O(n^2 log^3 |G| + t n log |G|)
// memory complexity : O(n^2 log |G| + tn)
// t : number of generators
// |G| : group size, obviously <= (n!)

vector<int> inv(vector<int>& p){
  vector<int> ret(p.size());
  for (int i = 0; i < p.size(); i++) ret[p[i]] = i;
  return ret;
}
vector<int> operator * (vector<int>& a, vector<int>& b ){
  vector<int> ret(a.size());
  for (int i = 0 ; i < a.size(); i++) ret[i] = b[a[i]];
  return ret;
}
// a group contains all subset products of generators
struct Group {
  int n, m;
  vector<vector<int>> lookup;
  vector<vector<vector<int>>> buckets, ibuckets;
  int yo(vector<int> p, bool add_to_group = 1){
    n = buckets.size();
    for (int i = 0; i < n ; i++){
      int res = lookup[i][p[i]];
      if (res == -1 ){
        if (add_to_group){
          buckets[i].push_back(p);
          ibuckets[i].push_back(inv(p));
          lookup[i][p[i]] = buckets[i].size() - 1;
        }
        return i;
      }
      p = p * ibuckets[i][res];
    }
    return -1;
  }
  ll size() {
    ll ret = 1;
    for (int i = 0; i < n; i++) {
        ret *= buckets[i].size();
        ret %= MOD;
    }
    return ret;
  }
  bool in_group(vector<int> g) { return yo(g, false) == -1; }
  Group(vector<vector<int>> &gen, int _n){
    n = _n, m = gen.size(); // m permutations of size n, 0 indexed
    lookup.resize(n);
    buckets.resize(n);
    ibuckets.resize(n);
    for (int i = 0; i < n ; i++){
      lookup[i].resize(n);
      fill(lookup[i].begin(), lookup[i].end(), -1);
    }
    vector<int> id(n);
    for (int i = 0; i < n ; i++) id[i] = i;
    for (int i = 0; i < n ; i++){
      buckets[i].push_back(id);
      ibuckets[i].push_back(id);
      lookup[i][i] = 0;
    }
    for (int i = 0; i < m ; i++) yo(gen[i]);
    queue<pair<pair<int, int>,pair<int, int>>> q;
    for (int i = 0; i < n ; i++) {
      for (int j = i; j < n ; j++) {
        for (int k = 0; k < buckets[i].size(); k++) {
          for (int l = 0; l < buckets[j].size(); l++) {
            q.push({pair<int, int>(i, k), pair<int, int>(j, l)});
          }
        }
      }
    }
    while(!q.empty()) {
      pair<int, int> a = q.front().first;
      pair<int, int> b = q.front().second;
      q.pop();
      int res = yo(buckets[a.first][a.second] * buckets[b.first][b.second]);
      if (res == -1) continue;
      pair<int, int> cur(res, (int)buckets[res].size() - 1);
      for (int i = 0; i < n; i ++) {
        for (int j = 0; j < (int)buckets[i].size(); ++j){
          if (i <= res) q.push(make_pair(pair<int, int>(i , j), cur));
          if (res <= i) q.push(make_pair(cur, pair<int, int>(i, j)));
        }
      }
    }
  }
};

ll pwr(ll a, ll k, ll p){
    if (k == 0) return 1;
    return (k % 2 == 0 ? pwr((a * a) % p, k / 2, p) : (a * pwr(a, k - 1, p)) % p);
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vvi V(k, vi(n));
    cin >> V;
    FOR(i,0,k) FOR(j,0,n) V[i][j]--;
    Group g(V, n);
    vvll dp(n, vll(n, 0));
    vvll dpi(n, vll(n, 0));
    FOR(i,0,n) FOR(j,i+1,n) dpi[i][j] = 1;
    vvll dp2(n, vll(n));
    vvll dpi2(n, vll(n));
    for(const auto& buk : g.buckets){
        //cout << "NEW BUCKET" << endl;
        FOR(i,0,n) FOR(j,i+1,n) dp2[i][j] = dpi2[i][j] = 0;
        for(const auto& perm : buk){
            //cout << perm << endl;
            FOR(i,0,n) FOR(j,i+1,n){
                int ni = perm[i];
                int nj = perm[j];
                if (ni < nj){
                    dp2[i][j] += dp[ni][nj];
                    dpi2[i][j] += dpi[ni][nj];
                } else {
                    dp2[i][j] += dpi[nj][ni];
                    dpi2[i][j] += dp[nj][ni];
                }
                if (dp2[i][j] >= MOD) dp2[i][j] -= MOD;
                if (dpi2[i][j] >= MOD) dpi2[i][j] -= MOD;
            }
        }
        swap(dp, dp2);
        swap(dpi, dpi2);
    }
    ll inv_tot = 0;
    FOR(i,0,n) FOR(j,i+1,n) {
        inv_tot += dp[i][j];
        if (inv_tot >= MOD) inv_tot -= MOD;
    }
    ll mian = pwr(g.size(), MOD - 2, MOD);
    cout << (inv_tot * mian) % MOD << endl;
}