#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = (1 << 11) + (1 << 10); const int M = 3005; int ojc[N * N + 10]; int sz[N * N + 10]; int inv_sz[N * N + 10]; bool vis[N * N + 10]; inline int idx(const int& i, const int& j) { return (i << 11) + (i << 10) + j; } inline int Find(const int& x) { if (ojc[x] == x) return x; return ojc[x] = Find(ojc[x]); } inline void Union(const int& xp, const int& yp) { int x = Find(xp); int y = Find(yp); if (x == y) return; if (sz[x] < sz[y]) { sz[y] += sz[x]; inv_sz[y] += inv_sz[x]; ojc[x] = y; } else { sz[x] += sz[y]; inv_sz[x] += inv_sz[y]; ojc[y] = x; } } ll fast_pow(ll a, ll w, const ll mod) { if (w == 0) { return 1LL; } if (w == 1) { return a % mod; } ll temp = fast_pow(a, w / 2, mod); temp *= temp; temp %= mod; if (w % 2 == 0) { return temp; } return (temp * a) % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ojc[idx(i, j)] = idx(i, j); sz[idx(i, j)] = 1; inv_sz[idx(i, j)] = (i < j) ? 0 : 1; } } vi perm(n); for (int x = 0; x < k; x++) { for (int j = 0; j < n; j++) { int a; cin >> a; a--; perm[j] = a; } for (int i = 0; i < n; i++) { int u = perm[i]; // if (u < i) { // continue; // } for (int j = 0; j < n; j++) { if (i == j) continue; int v = perm[j]; Union(idx(i, j), idx(u, v)); } } if (sz[Find(1)] == n * (n - 1)) { break; } } const ll mod = 1e9 + 7; ll nom = 0, den = 1; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int id = idx(i, j); if (vis[Find(id)]) continue; vis[Find(id)] = true; ll cur_nom = (ll)inv_sz[Find(id)] * (ll)(sz[Find(id)] - inv_sz[Find(id)]); ll cur_den = sz[Find(id)]; // cout << i << ", " << j << ": " << cur_nom << " " << cur_den << "\n"; // cout << cur_nom << " " << cur_den << "\n"; ll g = __gcd(cur_nom, cur_den); cur_nom /= g; cur_den /= g; cur_nom %= mod; cur_den %= mod; nom = (nom * cur_den + den * cur_nom) % mod; // cur_den = fast_pow(cur_den, mod - 2, mod); den = (den * cur_den) % mod; // cout << "> " << nom << " " << den << "\n"; // // assert(nom <= 1'000'000'000'000'000'000LL); // bigint big_g = gcd(nom, den); // nom /= big_g; // den /= big_g; } } // cout << "sum: " << nom << " " << den << "\n"; // nom %= mod; // den %= mod; cout << (nom * fast_pow(den, mod - 2, mod)) % mod << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = long double; using vi = vector<int>; using vll = vector<ll>; using vii = vector<pii>; const int N = (1 << 11) + (1 << 10); const int M = 3005; int ojc[N * N + 10]; int sz[N * N + 10]; int inv_sz[N * N + 10]; bool vis[N * N + 10]; inline int idx(const int& i, const int& j) { return (i << 11) + (i << 10) + j; } inline int Find(const int& x) { if (ojc[x] == x) return x; return ojc[x] = Find(ojc[x]); } inline void Union(const int& xp, const int& yp) { int x = Find(xp); int y = Find(yp); if (x == y) return; if (sz[x] < sz[y]) { sz[y] += sz[x]; inv_sz[y] += inv_sz[x]; ojc[x] = y; } else { sz[x] += sz[y]; inv_sz[x] += inv_sz[y]; ojc[y] = x; } } ll fast_pow(ll a, ll w, const ll mod) { if (w == 0) { return 1LL; } if (w == 1) { return a % mod; } ll temp = fast_pow(a, w / 2, mod); temp *= temp; temp %= mod; if (w % 2 == 0) { return temp; } return (temp * a) % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ojc[idx(i, j)] = idx(i, j); sz[idx(i, j)] = 1; inv_sz[idx(i, j)] = (i < j) ? 0 : 1; } } vi perm(n); for (int x = 0; x < k; x++) { for (int j = 0; j < n; j++) { int a; cin >> a; a--; perm[j] = a; } for (int i = 0; i < n; i++) { int u = perm[i]; // if (u < i) { // continue; // } for (int j = 0; j < n; j++) { if (i == j) continue; int v = perm[j]; Union(idx(i, j), idx(u, v)); } } if (sz[Find(1)] == n * (n - 1)) { break; } } const ll mod = 1e9 + 7; ll nom = 0, den = 1; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int id = idx(i, j); if (vis[Find(id)]) continue; vis[Find(id)] = true; ll cur_nom = (ll)inv_sz[Find(id)] * (ll)(sz[Find(id)] - inv_sz[Find(id)]); ll cur_den = sz[Find(id)]; // cout << i << ", " << j << ": " << cur_nom << " " << cur_den << "\n"; // cout << cur_nom << " " << cur_den << "\n"; ll g = __gcd(cur_nom, cur_den); cur_nom /= g; cur_den /= g; cur_nom %= mod; cur_den %= mod; nom = (nom * cur_den + den * cur_nom) % mod; // cur_den = fast_pow(cur_den, mod - 2, mod); den = (den * cur_den) % mod; // cout << "> " << nom << " " << den << "\n"; // // assert(nom <= 1'000'000'000'000'000'000LL); // bigint big_g = gcd(nom, den); // nom /= big_g; // den /= big_g; } } // cout << "sum: " << nom << " " << den << "\n"; // nom %= mod; // den %= mod; cout << (nom * fast_pow(den, mod - 2, mod)) % mod << "\n"; } |