#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using db = long double; using pi = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; #define mp make_pair #define pb push_back #define eb emplace_back #define x first #define y second #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define per(i,a,b) for(int i = (b) - 1; i >= (a); i--) #ifdef LOCAL template<class A, class B> auto& operator<<(auto &o, pair<A, B> p) { return o << '(' << p.x << ", " << p.y << ')'; } template<class... A> auto& operator<<(auto &o, tuple<A...> t) { bool is_first = true; o << "("; apply([&o, &is_first](auto... a) {((o << (is_first ? (is_first = false, "") : ", ") << a), ...);}, t); o << ")"; return o; } auto& operator<<(auto& o, auto a) { bool is_first = true; o << "{"; for (auto b : a) o << (is_first ? (is_first = false, "") : ", ") << b; return o << "}"; } void dump(auto... x) { bool is_first = true; ((cerr << (is_first ? (is_first = false, "") : ", ") << x), ...) << "\n"; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) ; #endif template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } constexpr ll MOD = 1'000'000'007; ll fpow(ll a, ll b) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; } return res; } ll solve_one_permutation(const vi &p) { const int n = sz(p); vector<bool> vis(n); vector<vi> cycles; rep(i, 0, n) { if (!vis[i]) { cycles.resize(sz(cycles) + 1); for (int j = i; !vis[j]; j = p[j]) { vis[j] = true; cycles.back().emplace_back(j); } } } ll res = 0; rep(i, 0, sz(cycles)) { rep(j, 0, sz(cycles)) { const auto gcd = __gcd(sz(cycles[i]), sz(cycles[j])); const auto lcm = sz(cycles[i]) * sz(cycles[j]) / gcd; rep(len, 0, gcd) { ll inv = 0, not_inv = 0; rep(a, 0, sz(cycles[i])) { for (int b = (a - len + gcd) % gcd; b < sz(cycles[j]); b += gcd) { if (cycles[i][a] > cycles[j][b]) inv++; else not_inv++; } } ll tmp = (inv * not_inv) % MOD; res = (res + tmp * fpow(lcm, MOD - 2)) % MOD; } } } return res; } class fenwick_tree { private: int n; vector<int> sum; public: fenwick_tree(int _n) : n(_n), sum(_n + 1) {} void add(int i, const int value) { for (i++; i <= n; i = (i | (i - 1)) + 1) { sum[i] += value; } } int get(int i) { int result = 0; for (i++; i > 0; i &= i - 1) { result += sum[i]; } return result; } }; ll get_inversions(const vi &p) { const int n = sz(p); fenwick_tree tree(n); ll res = 0; for (int i = n - 1; i >= 0; i--) { res += tree.get(p[i]); tree.add(p[i], 1); } return res; } vi compose(const vi &a, const vi &b) { vi res(sz(a)); rep(i, 0, sz(res)) { res[i] = a[b[i]]; } return res; } ll solve_many_permutations(const vector<vi> &p) { const int k = sz(p); const int n = sz(p[0]); vi id(n); iota(all(id), 0); set<vi> available; available.insert(id); bool any = true; while (any) { any = false; rep(i, 0, k) { vector<vi> new_available; for (const auto &other : available) { const auto maybe = compose(other, p[i]); if (available.find(maybe) == available.end()) { new_available.emplace_back(maybe); } } if (!new_available.empty()) { any = true; available.insert(all(new_available)); } } } ll res = 0; for (const auto &a : available) { res = (res + get_inversions(a)) % MOD; } res = (res * fpow(sz(available), MOD - 2)) % MOD; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; if (k == 1) { vi p(n); rep(i, 0, n) cin >> p[i], p[i]--; const auto res = solve_one_permutation(p); cout << res << "\n"; } else { vector<vi> p(k, vi(n)); rep(i, 0, k) rep(j, 0, n) cin >> p[i][j], p[i][j]--; const auto res = solve_many_permutations(p); cout << res << "\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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using db = long double; using pi = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; #define mp make_pair #define pb push_back #define eb emplace_back #define x first #define y second #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define per(i,a,b) for(int i = (b) - 1; i >= (a); i--) #ifdef LOCAL template<class A, class B> auto& operator<<(auto &o, pair<A, B> p) { return o << '(' << p.x << ", " << p.y << ')'; } template<class... A> auto& operator<<(auto &o, tuple<A...> t) { bool is_first = true; o << "("; apply([&o, &is_first](auto... a) {((o << (is_first ? (is_first = false, "") : ", ") << a), ...);}, t); o << ")"; return o; } auto& operator<<(auto& o, auto a) { bool is_first = true; o << "{"; for (auto b : a) o << (is_first ? (is_first = false, "") : ", ") << b; return o << "}"; } void dump(auto... x) { bool is_first = true; ((cerr << (is_first ? (is_first = false, "") : ", ") << x), ...) << "\n"; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) ; #endif template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } constexpr ll MOD = 1'000'000'007; ll fpow(ll a, ll b) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; } return res; } ll solve_one_permutation(const vi &p) { const int n = sz(p); vector<bool> vis(n); vector<vi> cycles; rep(i, 0, n) { if (!vis[i]) { cycles.resize(sz(cycles) + 1); for (int j = i; !vis[j]; j = p[j]) { vis[j] = true; cycles.back().emplace_back(j); } } } ll res = 0; rep(i, 0, sz(cycles)) { rep(j, 0, sz(cycles)) { const auto gcd = __gcd(sz(cycles[i]), sz(cycles[j])); const auto lcm = sz(cycles[i]) * sz(cycles[j]) / gcd; rep(len, 0, gcd) { ll inv = 0, not_inv = 0; rep(a, 0, sz(cycles[i])) { for (int b = (a - len + gcd) % gcd; b < sz(cycles[j]); b += gcd) { if (cycles[i][a] > cycles[j][b]) inv++; else not_inv++; } } ll tmp = (inv * not_inv) % MOD; res = (res + tmp * fpow(lcm, MOD - 2)) % MOD; } } } return res; } class fenwick_tree { private: int n; vector<int> sum; public: fenwick_tree(int _n) : n(_n), sum(_n + 1) {} void add(int i, const int value) { for (i++; i <= n; i = (i | (i - 1)) + 1) { sum[i] += value; } } int get(int i) { int result = 0; for (i++; i > 0; i &= i - 1) { result += sum[i]; } return result; } }; ll get_inversions(const vi &p) { const int n = sz(p); fenwick_tree tree(n); ll res = 0; for (int i = n - 1; i >= 0; i--) { res += tree.get(p[i]); tree.add(p[i], 1); } return res; } vi compose(const vi &a, const vi &b) { vi res(sz(a)); rep(i, 0, sz(res)) { res[i] = a[b[i]]; } return res; } ll solve_many_permutations(const vector<vi> &p) { const int k = sz(p); const int n = sz(p[0]); vi id(n); iota(all(id), 0); set<vi> available; available.insert(id); bool any = true; while (any) { any = false; rep(i, 0, k) { vector<vi> new_available; for (const auto &other : available) { const auto maybe = compose(other, p[i]); if (available.find(maybe) == available.end()) { new_available.emplace_back(maybe); } } if (!new_available.empty()) { any = true; available.insert(all(new_available)); } } } ll res = 0; for (const auto &a : available) { res = (res + get_inversions(a)) % MOD; } res = (res * fpow(sz(available), MOD - 2)) % MOD; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; if (k == 1) { vi p(n); rep(i, 0, n) cin >> p[i], p[i]--; const auto res = solve_one_permutation(p); cout << res << "\n"; } else { vector<vi> p(k, vi(n)); rep(i, 0, k) rep(j, 0, n) cin >> p[i][j], p[i][j]--; const auto res = solve_many_permutations(p); cout << res << "\n"; } return 0; } |