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
//GRT_2018
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define all(x) x.begin(), x.end()
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

const int nax = 3000 + 10, mod = 1e9 + 7;
vector<vi>perm;

vector<pi>g1[nax][nax];
vi V[nax * nax];
int ile[nax * nax];
int nr;
int id[nax][nax];
bool vis[nax][nax];

void dfs1(int i, int j) {
    vis[i][j] = true;
    ile[id[i][j]]++;
    for (auto [a,b] : g1[i][j]) {
        if (!vis[a][b]) {
            id[a][b] = id[i][j];
            dfs1(a, b);
        }
    }
}

bool vis2[nax * nax];
bool col[nax * nax];
int sz0, sz1;
bool bip;

void dfs(int x) {
    vis2[x] = true;
    if (col[x]) sz1 += ile[x];
    else sz0 += ile[x];

    for (int y : V[x]) {
        if (!vis2[y]) {
            col[y] = 1^col[x];
            dfs(y);
        } else if (col[y] == col[x]) {
            bip = false;
        }
    }
}

int fastpow(int a, int b) {
    int w = 1;
    while (b) {
        if (b & 1) w = ((ll)w * a) % mod;
        b >>= 1;
        a = ((ll)a * a) % mod;
    }
    return w;
}


int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
    int n, k;
    cin >> n >> k;
    perm.resize(k);
    for (int i = 0; i < k; ++i) {
        perm[i].resize(n);
        for (int &x : perm[i]) {
            cin >> x;
            x--;
        }
    }

    // handle the same
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            for (int ii = 0; ii < k; ++ii) {
                int a = perm[ii][i], b = perm[ii][j];
                if (a < b) {
                    g1[i][j].emplace_back(a, b);
                    g1[a][b].emplace_back(i, j);
                }
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (!vis[i][j]) {
                id[i][j] = ++nr;
                dfs1(i, j);
            }
        }
    }

    // handle different

    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            for (int ii = 0; ii < k; ++ii) {
                int a = perm[ii][i], b = perm[ii][j];
                if (a > b) {
                    V[id[i][j]].PB(id[b][a]);
                    V[id[b][a]].PB(id[i][j]);
                }
            }
        }
    }

    int ans = 0;
    for (int i = 1; i <= nr; ++i) {
        if (!vis2[i]) {
            bip = true;
            sz0 = sz1 = 0;
            dfs(i);
            if (!bip) {
                ans = (ans + (ll)(sz0 + sz1) * fastpow(2, mod - 2)) % mod;
            } else {
                int me = ((ll)sz0 * sz1) % mod;
                me = (2 * me) % mod;
                me = ((ll)me * fastpow(sz0 + sz1, mod - 2)) % mod;
                ans = (ans + me) % mod;
            }
        }
    }

    cout << ans;
}