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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include<bits/stdc++.h>

#define FOR(i,s,e) for(int i=(s); i<=(e); i++)
#define FORD(i,s,e) for(int i=(s); i>=(e); i--)
#define ALL(k) (k).begin(), (k).end()
#define e1 first
#define e2 second
#define mp make_pair
 
using namespace std;
using LL=long long;
using LD=long double;
using PII=pair<int,int>;
const int MAXN = 3111;
const LL MOD = ((LL)1e9)+7;

int perms[MAXN][MAXN];


LL power_mod(LL a, LL exp){
    LL res = 1;
    while(exp > 0){
        if (exp % 2){
            res = (res*a) % MOD;
        }
        exp /= 2;
        a *= a;
        a %= MOD;
    }
    return res;
}

LL mod_inverse(LL a){
    return power_mod(a, MOD-2);
}

// int perm_apply_a[MAXN];
// int perm_apply_b[MAXN];
// int perm_apply_c[MAXN];




// int apply_perm(int perm_length){
//     FOR(i,1,n){
//         perm_apply_c[i] = perm_apply_b[perm_apply_a[i]];
//     }
// }

vector<pair<int, vector<int>>> cycles;
set<int> considered_cycles;
set<PII> good_cycle_pairs;
set<int> poisoned_cycles;



vector<int> node_in_cycles[MAXN];

int unionfind[MAXN];
int poisoned_unionfind[MAXN];
int find(int v){
    if (unionfind[v] < 0){
        return v;
    }
    unionfind[v] = find(unionfind[v]);
    return unionfind[v];
}
bool join(int a, int b){
    int aa = find(a), bb = find(b);
    if (aa == bb){
        return false;
    }
    if (unionfind[aa] > unionfind[bb]){
        swap(aa, bb);
    }
    unionfind[aa] += unionfind[bb];
    unionfind[bb] = aa;
    return true;
}

int vis[MAXN];

int values[MAXN];

LL prob_to_go[MAXN];


int main(){
    int n, k; scanf("%d%d", &n, &k);
    // printf("n %d k %d\n", n, k);
    FOR(i,1,n){
        unionfind[i] = -1;
    }
    FOR(i,1,k){
        FOR(j, 1, n){
            scanf("%d", &perms[i][j]);
        }
    }
    FOR(i,1,k){
        // find all cycles
        FOR(j,1,n){
            vis[j] = 0;
        }
        FOR(j,1,n){
            if (perms[i][j] == j){
                continue;
            }
            if (vis[j]){
                continue;
            }

            // there's a cycle touching j
            int cycle_no = cycles.size();
            vector<int> current_cycle;
            current_cycle.push_back(j);
            node_in_cycles[j].push_back(cycle_no);
            int k = perms[i][j];
            while (k != j){
                current_cycle.push_back(k);
                node_in_cycles[k].push_back(cycle_no);
                vis[k] = 1;
                join(k, perms[i][k]);
                k = perms[i][k];
            }
            // and this cycle is described in current_cycle.
            cycles.emplace_back(i, current_cycle);
            // printf("Adding cycle no. %d within genperm %d: ", cycles.size()-1, i);
            // for(auto &it: current_cycle){
            //     printf("%d ", it);
            // }
            // printf("\n");
        }
    }
    // look at all cycles, and take one cycle for each node.
    // for each node, consider if its permutations are only within a group.

    FOR(j,1,n){
        int group_size = -unionfind[find(j)];
        map<int, vector<int>> cycle_signatures;
        for(auto &cycle_no : node_in_cycles[j]){
            if (cycles[cycle_no].e2.size() != group_size){
                poisoned_cycles.insert(cycle_no);
                poisoned_unionfind[find(j)] = 1;
            }
            int cycle_initial_perm = cycles[cycle_no].e1;
            int j_goes_to = perms[cycle_initial_perm][j];
            if (cycle_signatures.find(j_goes_to) != cycle_signatures.end()){
                cycle_signatures[j_goes_to].push_back(cycle_no);
            }
            else{
                cycle_signatures[j_goes_to] = vector<int>({cycle_no});
            }
        }
        
        if (find(j) != j){
            continue;
        }

        if (!poisoned_unionfind[find(j)] && node_in_cycles[j].size() > 1){
            // then all others must be in the group of node_in_cycles[0]
            int main_cycle_id = node_in_cycles[j][0];
            int main_cycle_initial_perm = cycles[main_cycle_id].e1;
            vector<int> main_cycle = cycles[main_cycle_id].e2;
            int cycle_length = main_cycle.size();

            // simulate cycle group generation, start with e
            for(auto &node : main_cycle){
                values[node] = node;
            }
            // then simulate each until back at one.
            FOR(l, 1, cycle_length){
                // run simulation
                for (auto &node : main_cycle){
                    values[node] = perms[main_cycle_initial_perm][values[node]];
                }
                int j_goes_to = values[j];

                // check signature
                for (auto signature: cycle_signatures[j_goes_to]){
                    bool is_okay = true;
                    for (auto &v : main_cycle){
                        if (perms[signature][v] != perms[main_cycle_initial_perm][v]){
                            is_okay = false;
                            break;
                        }
                    }
                    if (!is_okay){
                        poisoned_unionfind[find(j)] = 1;
                        break;
                    }
                }
                if (poisoned_unionfind[find(j)] == 1){
                    break;
                }
            }

        }
    }
    // printf("Poisoning unionfinds\n");
    // FOR(i,1,n){
    //     printf("%d: find %d fu %d poisoned %d\n", i, find(i), unionfind[i], poisoned_unionfind[i]);
    // }

    // poison all bad nodes
    FOR(i,1,n){
        if (poisoned_unionfind[find(i)]){
            poisoned_unionfind[i] = 1;
        }
    }

    // follow.
    // for poisoned, the inner result is |c|*|c-1|/4
    // for non-poisoned, the inner results comes from simulation
    // for multi, we'll use DP
    LL ans = 0;

    FOR(j,1,n){
        if (find(j) != j){
            continue;
        }
        if (unionfind[j] == -1){
            continue;
        }
        // poisoned - then just run on the size
        if (poisoned_unionfind[j]){
            int group_size = -unionfind[j];
            ans += (group_size * (LL)(group_size-1) * mod_inverse(4))%MOD;
            ans %= MOD;
        }
        // not poisoned - simulate group
        else{
            int main_cycle_id = node_in_cycles[j][0];
            int main_cycle_initial_perm = cycles[main_cycle_id].e1;
            vector<int> main_cycle = cycles[main_cycle_id].e2;
            int cycle_length = main_cycle.size();

            int local_inversions = 0;

            // simulate cycle group generation, start with e
            for(auto &node : main_cycle){
                values[node] = node;
            }
            // then simulate each until back at one.
            vector<int> sorted_cycle = main_cycle;
            sort(ALL(sorted_cycle));

            FOR(l, 1, cycle_length){
                // run simulation
                for (auto &node : main_cycle){
                    values[node] = perms[main_cycle_initial_perm][values[node]];
                }
                // calculate inversions
                FOR(i,0,sorted_cycle.size()-1){
                    FOR(j,i+1,sorted_cycle.size()-1){
                        if(values[sorted_cycle[j]] < values[sorted_cycle[i]]){
                            local_inversions++;
                        }
                    }
                }
            }
            ans += ((LL)(local_inversions) * mod_inverse(cycle_length))%MOD;
            ans %= MOD;
        }
    }

    // finally, do dynamic programming on disjoint instances.

    FOR(i,1,n){
        //for the position i,
        FOR(j,1,n){
            if (find(j) != find(i)) {
                continue;
            }
            LL j_group_size = -unionfind[find(j)];
            LL current_prob = mod_inverse(j_group_size);
            // for the value j in i's cycle, which has 1/|is cycle| probability of showing up
            FOR(k,j+1,n){
                // and value k outside of i's cycle
                if (find(k) == find(j)){
                    continue;
                }
                // add probability of k being earlier * probabiliy of j here;
                ans += (current_prob * prob_to_go[k])%MOD;
                ans %= MOD;
            }
            prob_to_go[j] += current_prob;
            prob_to_go[j] %= MOD;
        }
    }


    printf("%lld\n", ans);
}