//#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma warning(disable:4786) #pragma warning(disable:4996) #include <bits/stdc++.h> #include <random> #include <chrono> #include <unordered_set> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << to_string(H); dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) 0 #endif typedef long long int LL; //typedef __int128 LLL; typedef long long LLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<LL, int> PLI; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PLL> VPL; typedef vector<PII> VP; typedef vector<double> VD; typedef vector<vector<int>> VVI; typedef vector<vector<LL>> VVL; typedef vector<string> VS; typedef long double ld; typedef unsigned long long ULL; //#define MAXN 1000 //#define MAXN2 MAXN*MAXN // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // mt19937 rng(10); // shuffle(V.begin(), V.end(), rng); // int u = rng(); // int u = uniform_int_distribution<long long>(L, R)(rng); // double u = uniform_real_distribution<float>(L, R)(rng); // int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1}; // int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; const LL MOD = 1000000007; //const LL MOD = 998244353; // const LL INF = 2000000000000000001LL; //2e18 + 1 //typedef long long LL; //typedef unsigned long long ULL; int perm[3003][3003]; int n, k; PII cnt[3003][3003]; LL bigmod(LL a, LL b) { if (b == 0) return 1; LL x = bigmod(a, b / 2); x = (x * x) % MOD; if (b & 1) x = (x * a) % MOD; return x; } PII calc(int a, int b) { if (cnt[a][b].first != -1) return cnt[a][b]; queue<PII> Q; VP V; int inversion = 0; Q.push({ a, b }); if (a > b) inversion++; V.push_back({ a, b }); cnt[a][b] = { 0, 0 }; while (!Q.empty()) { PII u = Q.front(); Q.pop(); FORAB(i, 1, k) { PII v = { perm[i][u.first], perm[i][u.second] }; if (cnt[v.first][v.second].first == -1) { cnt[v.first][v.second] = { 0, 0 }; Q.push(v); V.push_back(v); if (v.first > v.second) inversion++; } } } for (auto& p : V) { cnt[p.first][p.second] = { SZ(V), inversion }; } return cnt[a][b]; } void solve(int ks) { scanf("%d %d", &n, &k); FORAB(i, 1, k) FORAB(j, 1, n) scanf("%d", &perm[i][j]); FORAB(i, 1, n) FORAB(j, 1, n) cnt[i][j] = { -1, -1 }; LL cycle_lengths = 1; VL V; FORAB(i, 1, n) FORAB(j, i + 1, n) { PII p = calc(i, j); cycle_lengths = (cycle_lengths * p.first) % MOD; V.push_back((bigmod(p.first, MOD - 2) * p.second) % MOD); } LL ans = 0; for (auto& v : V) { ans = (ans + (cycle_lengths * v)) % MOD; } ans = (ans * bigmod(cycle_lengths, MOD - 2)) % MOD; printf("%lld\n", ans); } void gen() { } int main() { // ios_base::sync_with_stdio(false); // cin.tie(nullptr); double start_time = clock(); #ifdef LOCAL freopen("C:\\Home\\Contest\\sample.in", "r", stdin); //freopen("C:\\Home\\Contest\\0.out", "w", stdout); #endif gen(); if (0) { int T; scanf("%d", &T); for (int ks = 1; ks <= T; ks++) { solve(ks); if (ks % 100 == 0) fprintf(stderr, "%d done\n", ks); //double end_time = clock(); //fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC); } } else { solve(1); } double end_time = clock(); fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC); 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 | //#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma warning(disable:4786) #pragma warning(disable:4996) #include <bits/stdc++.h> #include <random> #include <chrono> #include <unordered_set> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << to_string(H); dbg_out(T...); } #ifdef LOCAL #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) 0 #endif typedef long long int LL; //typedef __int128 LLL; typedef long long LLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<LL, int> PLI; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PLL> VPL; typedef vector<PII> VP; typedef vector<double> VD; typedef vector<vector<int>> VVI; typedef vector<vector<LL>> VVL; typedef vector<string> VS; typedef long double ld; typedef unsigned long long ULL; //#define MAXN 1000 //#define MAXN2 MAXN*MAXN // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // mt19937 rng(10); // shuffle(V.begin(), V.end(), rng); // int u = rng(); // int u = uniform_int_distribution<long long>(L, R)(rng); // double u = uniform_real_distribution<float>(L, R)(rng); // int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1}; // int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; const LL MOD = 1000000007; //const LL MOD = 998244353; // const LL INF = 2000000000000000001LL; //2e18 + 1 //typedef long long LL; //typedef unsigned long long ULL; int perm[3003][3003]; int n, k; PII cnt[3003][3003]; LL bigmod(LL a, LL b) { if (b == 0) return 1; LL x = bigmod(a, b / 2); x = (x * x) % MOD; if (b & 1) x = (x * a) % MOD; return x; } PII calc(int a, int b) { if (cnt[a][b].first != -1) return cnt[a][b]; queue<PII> Q; VP V; int inversion = 0; Q.push({ a, b }); if (a > b) inversion++; V.push_back({ a, b }); cnt[a][b] = { 0, 0 }; while (!Q.empty()) { PII u = Q.front(); Q.pop(); FORAB(i, 1, k) { PII v = { perm[i][u.first], perm[i][u.second] }; if (cnt[v.first][v.second].first == -1) { cnt[v.first][v.second] = { 0, 0 }; Q.push(v); V.push_back(v); if (v.first > v.second) inversion++; } } } for (auto& p : V) { cnt[p.first][p.second] = { SZ(V), inversion }; } return cnt[a][b]; } void solve(int ks) { scanf("%d %d", &n, &k); FORAB(i, 1, k) FORAB(j, 1, n) scanf("%d", &perm[i][j]); FORAB(i, 1, n) FORAB(j, 1, n) cnt[i][j] = { -1, -1 }; LL cycle_lengths = 1; VL V; FORAB(i, 1, n) FORAB(j, i + 1, n) { PII p = calc(i, j); cycle_lengths = (cycle_lengths * p.first) % MOD; V.push_back((bigmod(p.first, MOD - 2) * p.second) % MOD); } LL ans = 0; for (auto& v : V) { ans = (ans + (cycle_lengths * v)) % MOD; } ans = (ans * bigmod(cycle_lengths, MOD - 2)) % MOD; printf("%lld\n", ans); } void gen() { } int main() { // ios_base::sync_with_stdio(false); // cin.tie(nullptr); double start_time = clock(); #ifdef LOCAL freopen("C:\\Home\\Contest\\sample.in", "r", stdin); //freopen("C:\\Home\\Contest\\0.out", "w", stdout); #endif gen(); if (0) { int T; scanf("%d", &T); for (int ks = 1; ks <= T; ks++) { solve(ks); if (ks % 100 == 0) fprintf(stderr, "%d done\n", ks); //double end_time = clock(); //fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC); } } else { solve(1); } double end_time = clock(); fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC); return 0; } |