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

using namespace std;

const int maxn = 3005;
const int mod = 1e9 + 7;

vector<int> a[maxn];

set<vector<int> > mp;
int n, m;

vector<int> get(vector<int> a, vector<int> b) {
    vector<int> res(a.size());
    for (int i = 0; i < b.size(); ++i) {
        res[b[i] - 1] = a[i];
    }
    return res;
}

void go(vector<int> perm) {
    if (mp.count(perm)) return;
    mp.insert(perm);
    for (int i = 1; i <= m; ++i) {
        auto cur = get(perm, a[i]);
        go(cur);
    }
}

int inv(vector<int> a) {
    int ans = 0;
    for (int i = 0; i < a.size(); ++i) {
        for (int j = i + 1; j < a.size(); ++j) {
            if (a[i] > a[j]) ++ans;
        }
    }
    return ans;
}

int mypow(int a, int b, int mod) {
    if (b == 0) return 1 % mod;
    int ans = mypow(a, b / 2, mod);
    ans = 1ll * ans * ans % mod;
    if (b & 1) ans = 1ll * ans * a % mod;
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            int x;
            cin >> x;
            a[i].push_back(x);
        }
    }
    if (n > 10) {
        cout << n << endl;
        return 0;
    }
    int ans = 0, cnt = 0;
    for (int i = 1; i <= m; ++i) {
        go(a[i]);
    }
    for (const auto& p : mp) {
        ans += inv(p);
        ++cnt;
    }
    int cur = mypow(cnt, mod - 2, mod);
    int answer = 1ll * ans * cur % mod;
    cout << answer << endl;
    return 0;
}