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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const pair<ll, ll> P = make_pair(313LL, 367LL);
const pair<ll, ll> Q = make_pair(694202137LL, 1000LL * 1000LL * 1000LL + 7LL);
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 4 * 1000 * 1000 + 7;
pair<ll, ll> pot[30];
bool vis[N];
int per[N][11], il[N];
int giv[3 * 1000 + 7][11];
int sil[20];
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
    int operator()(int x) const { return x ^ RANDOM; }
};
gp_hash_table<ll, int, chash> num;

void DoP(int n)
{
    pot[0] = make_pair(1LL, 1LL);
    for(int i = 1; i <= n; ++i)
        pot[i] = make_pair((pot[i - 1].st * P.st) % Q.st, (pot[i - 1].nd * P.nd) % Q.nd);
}

ll QP(ll a, ll n)
{
    ll ans = 1LL;
    while(n > 0LL)
    {
        if(n % 2LL == 1LL) ans = (ans * a) % M;
        a = (a * a) % M;
        n /= 2LL;
    }
    return ans;
}

inline void Cnt(int n)
{
    bool prs[12]; int cur;
    for(int k = 1; k <= sil[n]; ++k)
    {
        cur = 0;
        for(int i = 1; i <= n; ++i) prs[i] = false;
        for(int i = 1; i <= n; ++i)
        {
            prs[per[k][i]] = true;
            for(int j = per[k][i] + 1; j <= n; ++j)
                if(prs[j]) ++cur;
        }
        //cout << "cnt " << k << " " << il[k] << "\n";
        il[k] = cur;
    }
}

inline void DoAns(int n)
{
    ll l = 0, m = 0;
    for(int i = 1; i <= sil[n]; ++i)
        if(vis[i])
        {
            ++m;
            l += il[i];
        }
    cout << (l * QP(m, M - 2LL)) % M << "\n";
}

void BFS(int n, int k)
{
    queue<int> q;
    int v;
    vis[1] = true;
    q.push(1);
    sil[0] = 1;
    while(q.size() > 0)
    {
        v = q.front(); q.pop();
        vector<int> nxt;
        //cout << "bfs: " << v << "\n";
        for(int j = 1; j <= k; ++j)
        {
            nxt.clear();
            for(int i = 1; i <= n; ++i)
                nxt.pb(per[v][giv[j][i]]);
            pair<ll, ll> curh = make_pair(0LL, 0LL);
            for(int i = 0; i < n; ++i)
            {
                curh.st = (curh.st + nxt[i] * pot[i].st) % Q.st;
                curh.nd = (curh.nd + nxt[i] * pot[i].nd) % Q.st;
            }

            ll ch = curh.st + (curh.nd<<31LL);
            int cur = num[ch];

            if(vis[cur]) continue;
            vis[cur] = true;
            q.push(cur);
        }
    }
}

void Solve()
{
    int n, k, cnt = 0;
    vector<int> cp;
    cin >> n >> k;
    for(int i = 1; i <= k; ++i)
        for(int j = 1; j <= n; ++j)
            cin >> giv[i][j];
    for(int i = 1; i <= n; ++i)
        cp.pb(i);
    do
    {
        ++cnt;
        pair<ll, ll> curh = make_pair(0LL, 0LL);
        for(int i = 1; i <= n; ++i) per[cnt][i] = cp[i - 1];
        for(int i = 0; i < n; ++i)
        {
            curh.st = (curh.st + cp[i] * pot[i].st) % Q.st;
            curh.nd = (curh.nd + cp[i] * pot[i].nd) % Q.st;
        }
        ll ch = curh.st + (curh.nd<<31LL);
        num[ch] = cnt;
    }while(next_permutation(cp.begin(), cp.end()));

    Cnt(n);
    BFS(n, k);
    DoAns(n);
}

int main()
{
    DoP(12);
    sil[0] = 1;
    for(int i = 1; i <= 15; ++i)
        sil[i] = sil[i - 1] * i;
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //int t; cin >> t;
    //while(t--)
        Solve();

    return 0;
}