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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#define st first
#define nd second
typedef long long ll;
using namespace std;

vector<int> mult(vector<int> a, vector<int> b) {
    vector<int> c(a.size());
    for (int i = 1;i < c.size(); i++) {
        c[i] = a[b[i]];
    }
    return c;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k; cin >> n >> k;
    vector<vector<int>> perms(k);
    for (int i = 0; i < k; i++) {
        perms[i].resize(n+1);
        for (int j = 1; j <= n; j++) cin >> perms[i][j];
    }
    set<vector<int>> vis;
    queue<vector<int>> bfs;
    for (int i = 0; i < k; i++) {
        bfs.push(perms[i]);
        vis.insert(perms[i]);
    }
    ll p= 0, q = 0;
    while (!bfs.empty()) {
        vector<int> perm = bfs.front();
        bfs.pop();
        q++;
        for (int i = 1; i <= n; i++) {
            for (int j = i+1; j <= n; j++) {
                if (perm[i] > perm[j]) p++;
            }
        }
        for (int i = 0; i < k; i++) {
            vector<int> c = mult(perm, perms[i]);
            if (vis.find(c) == vis.end()) {
                bfs.push(c);
                vis.insert(c);
            }
        }
    }
    ll inv = 1;
    ll mod = 1000000007;
    int pow = mod-2;
    while (pow) {
        if (pow&1) inv = (inv*q)%mod;
        pow/=2;
        q=(q*q)%mod;
    }
    cout << (p*inv)%mod << '\n';
}