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
// Spora część kodu porzyczona z: https://github.com/ShahjalalShohag/code-library/blob/main/Miscellaneous/Schreier%E2%80%93Sims%20algorithm.cpp
#include <bits/stdc++.h>
using namespace std;

#define sz(s) (int)s.size()
#define FOR(i,n) for (int i = 0; i < n; ++i)

using ll = long long;
using vi = vector<int>;

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

vi inv(vi &p)
{
    vi ret(sz(p));
    for (int i = 0; i < sz(p); ++i)
        ret[p[i]] = i;
    return ret;
}
vi operator*(vi &a, vi &b)
{
    vi ret(sz(a));
    for (int i = 0; i < sz(a); ++i)
        ret[i] = b[a[i]];
    return ret;
}
// a group contains all subset products of generators
struct Group
{
    int n, m;
    vector<vi> lookup;
    vector<vector<vi>> buckets, ibuckets;
    int yo(vi p, bool add_to_group = 1)
    {
        n = sz(buckets);
        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]] = sz(buckets[i]) - 1;
                }
                return i;
            }
            p = p * ibuckets[i][res];
        }
        return -1;
    }
    ll size()
    {
        ll ret = 1;
        for (int i = 0; i < n; ++i)
            ret *= sz(buckets[i]);
        return ret;
    }
    bool in_group(vi g) { return yo(g, false) == -1; }
    Group(vector<vi> &gen, int _n)
    {
        n = _n, m = sz(gen); // 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);
        }
        vi 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)
        {
            // cout << "BUCKET " << i << endl;
            for (int j = i; j < n; ++j)
            {
                for (int k = 0; k < sz(buckets[i]); ++k)
                {
                    for (int l = 0; l < sz(buckets[j]); ++l)
                    {
                        q.push({pair<int, int>(i, k), pair<int, int>(j, l)});
                    }
                }
            }
        }
        while (!q.empty())
        {
            // cout << "QUEUE SIZE: " << q.size() << endl;
            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, sz(buckets[res]) - 1);
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < sz(buckets[i]); ++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 modpow(ll a, ll b)
{
    ll ret = 1;
    while (b)
    {
        if (b & 1)
            ret = ret * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ret;
}

ll modinv(ll a)
{
    return modpow(a, MOD - 2);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int k, n;
    cin >> n >> k;
    vector<vi> a;
    while (k--)
    {
        vi v;
        for (int i = 0; i < n; ++i)
        {
            int x;
            cin >> x;
            v.push_back(x - 1);
        }
        a.push_back(v);
    }
    // cout << "START PREPROC: " << endl;
    Group g(a, n);
    // cout << "END PREPROC: " << endl;

    // cout << "GROUP I-BUCKETS\n";
    // for (auto &u : g.ibuckets)
    // {
    //     cout << "NEW I-BUCKET\n";
    //     for (auto &v : u)
    //     {
    //         for (auto &x : v)
    //         {
    //             cout << x << ' ';
    //         }
    //         cout << '\n';
    //     }
    // }
    // cout <<  "------------" << endl;

    vector<vector<ll>> dp(n, vector<ll>(n));
    FOR(i, n)
    {
        for (int j = i+1; j < n; j++)
        {
            dp[i][j] = 1;
        }
    }

    ll ile_perm = 1;
    FOR (i, n)
    {
        vector<vector<ll>> nowe_dp(n, vector<ll>(n));

        ile_perm *= sz(g.ibuckets[i]);
        ile_perm %= MOD;

        FOR (poz_mniejsza, n)
        {
            FOR (poz_wieksza, n)
            {
                if (poz_mniejsza == poz_wieksza)
                {
                    continue;
                }

                for (auto & perm: g.ibuckets[i])
                {
                    nowe_dp[perm[poz_mniejsza]][perm[poz_wieksza]] += dp[poz_mniejsza][poz_wieksza];
                }
            }
        }

        // cout << "NOWE DP: " << endl;
        FOR (poz_mniejsza, n)
        {
            FOR (poz_wieksza, n)
            {
                nowe_dp[poz_mniejsza][poz_wieksza] %= MOD;
                // cout << nowe_dp[poz_mniejsza][poz_wieksza] << ' ';
            }
            // cout << endl;
        }
        // cout << endl;
        swap(dp, nowe_dp);
    }

    ll suma_inv = 0;

    FOR (poz_mniejsza, n)
    {
        for (int poz_wieksza = 0; poz_wieksza < poz_mniejsza; ++poz_wieksza)
        {
            // cout << poz_mniejsza << ' ' << poz_wieksza << ' ' << dp[poz_mniejsza][poz_wieksza] << endl;
            suma_inv += dp[poz_mniejsza][poz_wieksza];
            suma_inv %= MOD;
        }
    }
    // cout << suma_inv << endl;
    cout << suma_inv * modinv(ile_perm) % MOD << '\n';
    return 0;
}