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
#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i =(a); i < (b); ++i)
#define re(i, n) FOR(i, 1, n)
#define ford(i, a, b) for(int i = (a); i >= (b); --i)
#define rep(i, n) for(int i = 0;i <(n); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pll> vpll;
typedef vector<pii> vpii;

const ll inf = 1e18;
const int N = 5005;
const int mod = 1e9 + 7;

// 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!)

//zajebane z https://github.com/ShahjalalShohag/code-library/blob/main/Miscellaneous/Schreier%E2%80%93Sims%20algorithm.cpp

ll ffpow(ll x, ll exp)
{
    x %= mod;
    ll ret = 1ll;
    while (exp)
    {
        if (exp & 1)
        {
            ret *= x;
            ret %= mod;
        }
        x *= x;
        x %= mod;
        exp >>= 1;
    }
    return ret;
}

ll minv(ll x)
{
    return ffpow(x, mod - 2);
}

// 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;
}

void comp (vector<int>& a, vector<int>& b, vector<int>& ret){
  ret.resize(a.size());
  for (int i = 0 ; i < a.size(); i++) ret[i] = b[a[i]];
}
// 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();
    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)));
        }
      }
    }
  }
};

int n, k;
vector<vi> gen;
vector<pair<int, vi>> dupa;

ll cnt2 = 0ll;


int inverrsss(vi perm)
{
    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < i; j++) cnt += (perm[j] > perm[i]);
    }
    return cnt;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    gen.resize(k);
    rep(i, k)
    {
        gen[i].resize(n);
        rep(j, n) 
        {
            cin >> gen[i][j];
            gen[i][j]--;
        }
    }
    Group perm_g = Group(gen, n);
    vi id(n);
    rep(i, n) id[i] = i;
    dupa.push_back({0, id});
    rep(i, n)
    {
        int v = i&1;
        int u = (i+1)&1;
        int kmax = sz(dupa);
        for (int k = 0; k < kmax; k++)
        {
            vi perm = dupa[k].second;
            for (int j = sz(perm_g.buckets[i]) - 1; j >= 0 ; j--)
            {
                vi perm2;
                comp(perm_g.buckets[i][j], perm, perm2);
                int cnt = dupa[k].first;
                for (int j = 0; j < i; j++)
                {
                    if (perm2[j] > perm2[i])
                    {
                        cnt++;
                        if (cnt >= mod) cnt -= mod;
                    }
                }
                if (j == 0) dupa[k].first = cnt;
                else
                {
                    dupa.push_back({cnt, perm2});
                }
            }
        }
    }
    for (auto& v : dupa)
    {
        cnt2 += v.first;
        if (cnt2 >= mod) cnt2 -= mod;
    }
    ll m = dupa.size();
    cout << (cnt2 * minv(m)) % mod << '\n';
}