//#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char spk(...); template <class c> auto spk(c *a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if<sizeof spk<c>(0) != 1, stream &>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if<sizeof spk<c>(0) == 1, stream &>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream &operator<<(pair<a, b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream &operator<<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream &_dbg(const string &s, int i, int b) { return *this; } template <class c, class... cs> stream &_dbg(const string &s, int i, int b, c arg, cs... args) { if (i == (int)(s.size())) return (*this << ": " << arg); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') - (s[i] == ']') - (s[i] == '}'); return (s[i] == ',' && b == 0) ? (*this << ": " << arg << " ")._dbg(s, i + 1, b, args...) : (s[i] == ' ' ? *this : *this << s[i]) ._dbg(s, i + 1, b, arg, args...); } }; } // namespace debug #ifdef DEBUG #define dout debug::stream() #define dbg(...) \ ((dout << "line:" << __LINE__ << " >> ") \ ._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) #else #define dout #define dbg(...) #endif // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // **************************************************************************** vector<vector<int>> CycleDecomposition(const vector<int> &perm) { int n = perm.size(); vector<int> vis(n, -1); vector<int> cycle; vector<vector<int>> decomposition; for (int i = 0; i < n; i++) { cycle.clear(); if (vis[i] == -1) { int j = i; while (vis[j] == -1) { cycle.push_back(j); vis[j] = i; j = perm[j]; } if (cycle.size() > 1) { decomposition.push_back(cycle); } } } return decomposition; } vector<pair<int, int>> DecomposeSwapIntoAdjacentTranspositions(int a, int b) { vector<pair<int, int>> decomposition; if (a > b) swap(a, b); for (int i = a; i < b; i++) { decomposition.push_back({i, i + 1}); } for (int i = b - 1; i > a; i--) { decomposition.push_back({i - 1, i}); } return decomposition; } vector<pair<int, int>> TranspositionDecomposition(const vector<int> &cycle) { vector<pair<int, int>> decomposition; for (int i = 0; i + 1 < cycle.size(); i++) { int a = cycle[i]; int b = cycle[i + 1]; vector<pair<int, int>> t = DecomposeSwapIntoAdjacentTranspositions(a, b); decomposition.insert(decomposition.end(), t.begin(), t.end()); } return decomposition; } vector<pair<int, int>> PermutationTranspositionDecomposition(const vector<int> &perm) { vector<vector<int>> cycles = CycleDecomposition(perm); vector<pair<int, int>> decomposition; for (const auto &cycle : cycles) { vector<pair<int, int>> d = TranspositionDecomposition(cycle); decomposition.insert(decomposition.end(), d.begin(), d.end()); } return decomposition; } // **************************************************************************** vector<int> Apply(const vector<int> &A, const vector<int> &B) { // assert(A.size() == B.size()); // assert(!A.empty()); vector<int> C(A.size()); for (int i = 0; i < A.size(); i++) { C[i] = A[B[i]]; } return C; } vector<int> Inverse(const vector<int> &A) { vector<int> B(A.size()); for (int i = 0; i < A.size(); i++) { B[A[i]] = i; } return B; } void BuildSchreierTree(int i, const set<vector<int>> &S, vector<vector<int>> *orbit) { for (const vector<int> &g : S) { if ((*orbit)[g[i]].empty()) { (*orbit)[g[i]] = Apply(g, (*orbit)[i]); BuildSchreierTree(g[i], S, orbit); } } } set<vector<int>> MakeGen(const set<vector<int>> &S, const vector<vector<int>> &orbit, int n) { set<vector<int>> new_S; for (const vector<int> &s : S) { for (int u = 0; u < n; u++) { if (orbit[u].empty()) continue; new_S.insert(Apply(Apply(Inverse(orbit[s[u]]), s), orbit[u])); } } return new_S; } set<vector<int>> Normalize(const set<vector<int>> &S, int i, int n) { set<vector<int>> new_S; vector<map<int, vector<int>>> base(n); int decl_cnt = 0; for (vector<int> s : S) { bool declined = true; for (int x = i + 1; x < n; x++) { if (s[x] == x) continue; if (base[x].find(s[x]) != base[x].end()) { s = Apply(Inverse(s), base[x][s[x]]); } else { base[x][s[x]] = s; new_S.insert(s); declined = false; break; } } decl_cnt += declined; if (decl_cnt >= 5) { break; } } return new_S; } vector<vector<vector<int>>> SchreierSims(set<vector<int>> S, int n) { if (S.empty()) { return {}; } vector<vector<vector<int>>> res; for (int i = 0; i < n && !S.empty(); i++) { vector<vector<int>> orbit(n); orbit[i] = vector<int>(n); iota(orbit[i].begin(), orbit[i].end(), 0); BuildSchreierTree(i, S, &orbit); res.push_back({}); for (int j = 0; j < n; j++) { if (!orbit[j].empty()) { res.back().push_back(orbit[j]); } } S = Normalize(MakeGen(S, orbit, n), i, n); } return res; } const int MOD = 1e9 + 7; int FastPow(int a, int n) { int res = 1; while (n) { if (n & 1) { res = (long long)res * a % MOD; } a = (long long)a * a % MOD; n >>= 1; } return res; } int Inversions(const vector<int> &A) { int n = A.size(); int inv = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (A[i] > A[j]) { inv++; } } } return inv; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; set<vector<int>> S; for (int i = 0; i < k; i++) { vector<int> p(n); for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; } S.insert(p); } vector<vector<vector<int>>> res = SchreierSims(S, n); // L - left is smaller // R - right is smaller vector<vector<long long>> L(n, vector<long long>(n, 0)); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { L[i][j] = 1; } } long long total_inv = 0; long long subgroup_size = 1; // dbg(L); // set<vector<int>> generated; // vector<int> I(n); // iota(I.begin(), I.end(), 0); // generated.insert(I); // reverse(res.begin(), res.end()); for (const auto &gen : res) { // dbg(gen); subgroup_size = (long long)subgroup_size * gen.size() % MOD; vector<vector<long long>> L_acc(n, vector<long long>(n, 0)); // set<vector<int>> generated_ac; // long long pref_total_inv = 0; // dbg(L); long long total_sum = 0; for (const auto &perm : gen) { long long pref_sum = total_inv; // for (const auto &g : generated) { // vector<int> perm_g = Apply(g, perm); // pref_total_inv += Inversions(perm_g); // generated_ac.insert(perm_g); // dbg(perm); // dbg(g); // dbg(perm_g); // } vector<vector<long long>> Lcp = L; vector<pair<int, int>> d = PermutationTranspositionDecomposition(perm); // reverse(d.begin(), d.end()); for (const auto &t : d) { // assert (t.first < t.second); pref_sum += Lcp[t.first][t.second]; pref_sum -= Lcp[t.second][t.first]; // dbg(t); for (int i = 0; i < n; i++) { if (i == t.first || i == t.second) continue; swap(Lcp[i][t.first], Lcp[i][t.second]); swap(Lcp[t.first][i], Lcp[t.second][i]); } swap(Lcp[t.first][t.second], Lcp[t.second][t.first]); // dbg(Lcp); } pref_sum %= MOD; pref_sum = (pref_sum + MOD) % MOD; total_sum = (total_sum + pref_sum) % MOD; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { L_acc[i][j] += Lcp[i][j]; L_acc[i][j] %= MOD; } } // dbg(perm); // dbg(total_inv); // dbg(pref_total_inv); // dbg(Lcp); } total_inv = total_sum; // generated = generated_ac; // dbg(generated.size()); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { L[i][j] = L_acc[i][j] % MOD; } } // dbg(pref_total_inv); // dbg(total_inv); // dbg(L); // cerr << '\n'; } // dbg(subgroup_size); int inv = FastPow(subgroup_size, MOD - 2); // cout << total_inv << " " << subgroup_size << '\n'; cout << total_inv % MOD * 1LL * inv % MOD << '\n'; return 0; }
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | //#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char spk(...); template <class c> auto spk(c *a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if<sizeof spk<c>(0) != 1, stream &>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if<sizeof spk<c>(0) == 1, stream &>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream &operator<<(pair<a, b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream &operator<<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream &_dbg(const string &s, int i, int b) { return *this; } template <class c, class... cs> stream &_dbg(const string &s, int i, int b, c arg, cs... args) { if (i == (int)(s.size())) return (*this << ": " << arg); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') - (s[i] == ']') - (s[i] == '}'); return (s[i] == ',' && b == 0) ? (*this << ": " << arg << " ")._dbg(s, i + 1, b, args...) : (s[i] == ' ' ? *this : *this << s[i]) ._dbg(s, i + 1, b, arg, args...); } }; } // namespace debug #ifdef DEBUG #define dout debug::stream() #define dbg(...) \ ((dout << "line:" << __LINE__ << " >> ") \ ._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) #else #define dout #define dbg(...) #endif // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // **************************************************************************** vector<vector<int>> CycleDecomposition(const vector<int> &perm) { int n = perm.size(); vector<int> vis(n, -1); vector<int> cycle; vector<vector<int>> decomposition; for (int i = 0; i < n; i++) { cycle.clear(); if (vis[i] == -1) { int j = i; while (vis[j] == -1) { cycle.push_back(j); vis[j] = i; j = perm[j]; } if (cycle.size() > 1) { decomposition.push_back(cycle); } } } return decomposition; } vector<pair<int, int>> DecomposeSwapIntoAdjacentTranspositions(int a, int b) { vector<pair<int, int>> decomposition; if (a > b) swap(a, b); for (int i = a; i < b; i++) { decomposition.push_back({i, i + 1}); } for (int i = b - 1; i > a; i--) { decomposition.push_back({i - 1, i}); } return decomposition; } vector<pair<int, int>> TranspositionDecomposition(const vector<int> &cycle) { vector<pair<int, int>> decomposition; for (int i = 0; i + 1 < cycle.size(); i++) { int a = cycle[i]; int b = cycle[i + 1]; vector<pair<int, int>> t = DecomposeSwapIntoAdjacentTranspositions(a, b); decomposition.insert(decomposition.end(), t.begin(), t.end()); } return decomposition; } vector<pair<int, int>> PermutationTranspositionDecomposition(const vector<int> &perm) { vector<vector<int>> cycles = CycleDecomposition(perm); vector<pair<int, int>> decomposition; for (const auto &cycle : cycles) { vector<pair<int, int>> d = TranspositionDecomposition(cycle); decomposition.insert(decomposition.end(), d.begin(), d.end()); } return decomposition; } // **************************************************************************** vector<int> Apply(const vector<int> &A, const vector<int> &B) { // assert(A.size() == B.size()); // assert(!A.empty()); vector<int> C(A.size()); for (int i = 0; i < A.size(); i++) { C[i] = A[B[i]]; } return C; } vector<int> Inverse(const vector<int> &A) { vector<int> B(A.size()); for (int i = 0; i < A.size(); i++) { B[A[i]] = i; } return B; } void BuildSchreierTree(int i, const set<vector<int>> &S, vector<vector<int>> *orbit) { for (const vector<int> &g : S) { if ((*orbit)[g[i]].empty()) { (*orbit)[g[i]] = Apply(g, (*orbit)[i]); BuildSchreierTree(g[i], S, orbit); } } } set<vector<int>> MakeGen(const set<vector<int>> &S, const vector<vector<int>> &orbit, int n) { set<vector<int>> new_S; for (const vector<int> &s : S) { for (int u = 0; u < n; u++) { if (orbit[u].empty()) continue; new_S.insert(Apply(Apply(Inverse(orbit[s[u]]), s), orbit[u])); } } return new_S; } set<vector<int>> Normalize(const set<vector<int>> &S, int i, int n) { set<vector<int>> new_S; vector<map<int, vector<int>>> base(n); int decl_cnt = 0; for (vector<int> s : S) { bool declined = true; for (int x = i + 1; x < n; x++) { if (s[x] == x) continue; if (base[x].find(s[x]) != base[x].end()) { s = Apply(Inverse(s), base[x][s[x]]); } else { base[x][s[x]] = s; new_S.insert(s); declined = false; break; } } decl_cnt += declined; if (decl_cnt >= 5) { break; } } return new_S; } vector<vector<vector<int>>> SchreierSims(set<vector<int>> S, int n) { if (S.empty()) { return {}; } vector<vector<vector<int>>> res; for (int i = 0; i < n && !S.empty(); i++) { vector<vector<int>> orbit(n); orbit[i] = vector<int>(n); iota(orbit[i].begin(), orbit[i].end(), 0); BuildSchreierTree(i, S, &orbit); res.push_back({}); for (int j = 0; j < n; j++) { if (!orbit[j].empty()) { res.back().push_back(orbit[j]); } } S = Normalize(MakeGen(S, orbit, n), i, n); } return res; } const int MOD = 1e9 + 7; int FastPow(int a, int n) { int res = 1; while (n) { if (n & 1) { res = (long long)res * a % MOD; } a = (long long)a * a % MOD; n >>= 1; } return res; } int Inversions(const vector<int> &A) { int n = A.size(); int inv = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (A[i] > A[j]) { inv++; } } } return inv; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; set<vector<int>> S; for (int i = 0; i < k; i++) { vector<int> p(n); for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; } S.insert(p); } vector<vector<vector<int>>> res = SchreierSims(S, n); // L - left is smaller // R - right is smaller vector<vector<long long>> L(n, vector<long long>(n, 0)); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { L[i][j] = 1; } } long long total_inv = 0; long long subgroup_size = 1; // dbg(L); // set<vector<int>> generated; // vector<int> I(n); // iota(I.begin(), I.end(), 0); // generated.insert(I); // reverse(res.begin(), res.end()); for (const auto &gen : res) { // dbg(gen); subgroup_size = (long long)subgroup_size * gen.size() % MOD; vector<vector<long long>> L_acc(n, vector<long long>(n, 0)); // set<vector<int>> generated_ac; // long long pref_total_inv = 0; // dbg(L); long long total_sum = 0; for (const auto &perm : gen) { long long pref_sum = total_inv; // for (const auto &g : generated) { // vector<int> perm_g = Apply(g, perm); // pref_total_inv += Inversions(perm_g); // generated_ac.insert(perm_g); // dbg(perm); // dbg(g); // dbg(perm_g); // } vector<vector<long long>> Lcp = L; vector<pair<int, int>> d = PermutationTranspositionDecomposition(perm); // reverse(d.begin(), d.end()); for (const auto &t : d) { // assert (t.first < t.second); pref_sum += Lcp[t.first][t.second]; pref_sum -= Lcp[t.second][t.first]; // dbg(t); for (int i = 0; i < n; i++) { if (i == t.first || i == t.second) continue; swap(Lcp[i][t.first], Lcp[i][t.second]); swap(Lcp[t.first][i], Lcp[t.second][i]); } swap(Lcp[t.first][t.second], Lcp[t.second][t.first]); // dbg(Lcp); } pref_sum %= MOD; pref_sum = (pref_sum + MOD) % MOD; total_sum = (total_sum + pref_sum) % MOD; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { L_acc[i][j] += Lcp[i][j]; L_acc[i][j] %= MOD; } } // dbg(perm); // dbg(total_inv); // dbg(pref_total_inv); // dbg(Lcp); } total_inv = total_sum; // generated = generated_ac; // dbg(generated.size()); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { L[i][j] = L_acc[i][j] % MOD; } } // dbg(pref_total_inv); // dbg(total_inv); // dbg(L); // cerr << '\n'; } // dbg(subgroup_size); int inv = FastPow(subgroup_size, MOD - 2); // cout << total_inv << " " << subgroup_size << '\n'; cout << total_inv % MOD * 1LL * inv % MOD << '\n'; return 0; } |