#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif #define my_getchar() (S == T && (T = (S = BB) + fread_unlocked(BB, 1, 1 << 15, stdin), S == T) ? EOF : *S++) char BB[1 << 20], *S = BB, *T = BB; inline void read(int &x) { x = 0; char c = my_getchar(); while (isdigit(c)) { x = 10 * x + (c & 15); c = my_getchar(); } } const int B = 12; const int maxN = 3000; const int maxV = (maxN << B); int col[maxV + 1]; vi g[maxV]; int uni_cnt = 0; void __attribute__ ((noinline)) uni(int v, int u, int who) { if (who) { uni(v, u, 0); } else { v = (v >> B) + ((v % (1 << B)) << B); u = (u >> B) + ((u % (1 << B)) << B); } uni_cnt += 1; v = col[v]; u = col[u]; if (v == u) return; if (sz(g[v]) < sz(g[u])) swap(v, u); for (auto w : g[u]) { col[w] = v; g[v].push_back(w); } g[u].clear(); g[u].shrink_to_fit(); } int pot(int a, int w, int mod) { int res = 1; while (w) { if (w & 1) res = 1ll * res * a % mod; w /= 2; a = 1ll * a * a % mod; } return res; } int32_t main() { int n, k; read(n); read(k); int N = n << B; iota(col, col + N, 0); rep(i, N) g[i].push_back(i); int p[maxN]; rep(i, k) { rep(j, n) { read(p[j]); p[j] -= 1; } for (int a = 0; a < n; a ++) { int aB = a << B; int pa = p[a]; int paB = p[a] << B; for (int b = a - 4; b >= 0; b -= 4) { if (col[aB + b] != col[paB + p[b]]) { uni(aB + b, paB + p[b], 1); } if (col[aB + b + 1] != col[paB + p[b + 1]]) { uni(aB + b + 1, paB + p[b + 1], 1); } if (col[aB + b + 2] != col[paB + p[b + 2]]) { uni(aB + b + 2, paB + p[b + 2], 1); } if (col[aB + b + 3] != col[paB + p[b + 3]]) { uni(aB + b + 3, paB + p[b + 3], 1); } } if (a % 4) { if (col[aB] != col[paB + p[0]]) { uni(aB, paB + p[0], 1); } } if (a % 4 >= 2) { if (col[aB + 1] != col[paB + p[1]]) { uni(aB + 1, paB + p[1], 1); } } if (a % 4 == 3) { if (col[aB + 2] != col[paB + p[2]]) { uni(aB + 2, paB + p[2], 1); } } } } const int mod = 1e9 + 7; int ans = 0; col[0] = 1; fwd(i, 1, N + 1) col[i] = 1ll * col[i - 1] * i % mod; col[N] = pot(col[N], mod - 2, mod); for (int i = N; i; i --) { int inv = 1ll * col[i] * col[i - 1] % mod; col[i - 1] = 1ll * col[i] * i % mod; col[i] = inv; } rep(i, N) { int a = 0; int b = 0; for (auto v : g[i]) { int l = (v >> B); int r = v % (1 << B); if (l < r) { a += 1; } else if (l > r) { b += 1; } } int cont = (1ll * a * b % mod) * col[a + b] % mod; ans = (ans + cont) % mod; } printf("%d\n", ans); 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 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif #define my_getchar() (S == T && (T = (S = BB) + fread_unlocked(BB, 1, 1 << 15, stdin), S == T) ? EOF : *S++) char BB[1 << 20], *S = BB, *T = BB; inline void read(int &x) { x = 0; char c = my_getchar(); while (isdigit(c)) { x = 10 * x + (c & 15); c = my_getchar(); } } const int B = 12; const int maxN = 3000; const int maxV = (maxN << B); int col[maxV + 1]; vi g[maxV]; int uni_cnt = 0; void __attribute__ ((noinline)) uni(int v, int u, int who) { if (who) { uni(v, u, 0); } else { v = (v >> B) + ((v % (1 << B)) << B); u = (u >> B) + ((u % (1 << B)) << B); } uni_cnt += 1; v = col[v]; u = col[u]; if (v == u) return; if (sz(g[v]) < sz(g[u])) swap(v, u); for (auto w : g[u]) { col[w] = v; g[v].push_back(w); } g[u].clear(); g[u].shrink_to_fit(); } int pot(int a, int w, int mod) { int res = 1; while (w) { if (w & 1) res = 1ll * res * a % mod; w /= 2; a = 1ll * a * a % mod; } return res; } int32_t main() { int n, k; read(n); read(k); int N = n << B; iota(col, col + N, 0); rep(i, N) g[i].push_back(i); int p[maxN]; rep(i, k) { rep(j, n) { read(p[j]); p[j] -= 1; } for (int a = 0; a < n; a ++) { int aB = a << B; int pa = p[a]; int paB = p[a] << B; for (int b = a - 4; b >= 0; b -= 4) { if (col[aB + b] != col[paB + p[b]]) { uni(aB + b, paB + p[b], 1); } if (col[aB + b + 1] != col[paB + p[b + 1]]) { uni(aB + b + 1, paB + p[b + 1], 1); } if (col[aB + b + 2] != col[paB + p[b + 2]]) { uni(aB + b + 2, paB + p[b + 2], 1); } if (col[aB + b + 3] != col[paB + p[b + 3]]) { uni(aB + b + 3, paB + p[b + 3], 1); } } if (a % 4) { if (col[aB] != col[paB + p[0]]) { uni(aB, paB + p[0], 1); } } if (a % 4 >= 2) { if (col[aB + 1] != col[paB + p[1]]) { uni(aB + 1, paB + p[1], 1); } } if (a % 4 == 3) { if (col[aB + 2] != col[paB + p[2]]) { uni(aB + 2, paB + p[2], 1); } } } } const int mod = 1e9 + 7; int ans = 0; col[0] = 1; fwd(i, 1, N + 1) col[i] = 1ll * col[i - 1] * i % mod; col[N] = pot(col[N], mod - 2, mod); for (int i = N; i; i --) { int inv = 1ll * col[i] * col[i - 1] % mod; col[i - 1] = 1ll * col[i] * i % mod; col[i] = inv; } rep(i, N) { int a = 0; int b = 0; for (auto v : g[i]) { int l = (v >> B); int r = v % (1 << B); if (l < r) { a += 1; } else if (l > r) { b += 1; } } int cont = (1ll * a * b % mod) * col[a + b] % mod; ans = (ans + cont) % mod; } printf("%d\n", ans); return 0; } |