#include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int MM = 3013; int par[MM*MM]; int L[MM*MM]; int inv[MM*MM]; int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); } void Union(int a, int b) { a = find(a); b = find(b); if(a == b) return; if(par[a] > par[b]) swap(a,b); par[a] += par[b]; par[b] = a; L[a] += L[b]; } const int P = 1e9 + 7; int mul(int a, int b) { return 1LL*a*b%P; } int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif int n,k; cin >> n >> k; fill(par,par+n*n,-1); inv[1] = 1; rep(i,2,n*n + 1) inv[i] = mul(P/i, P - inv[P%i]); rep(i,0,n) rep(j,i+1,n) L[i*n+j] = 1; Vi T(n); while(k--) { for(auto&v:T) cin >> v, --v; rep(i,0,n) rep(j,0,n) if(i!=j) Union(i*n+j,T[i]*n+T[j]); if(n != 1 && -par[find(1)] == n*(n-1)) break; } ll res = 0; rep(i,0,n) rep(j,0,n) if(i!=j) { int ind = i*n+j; if(par[ind] < 0) { int orb_size = -par[ind]; res += mul(inv[orb_size],mul(L[ind],orb_size-L[ind])); } } cout << res % P; 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 | #include <bits/stdc++.h> #define fi first #define sc second #define pb push_back #define rep(i,p,k) for(int i=(p);i<(k);++i) #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k)-1;i>=(p);--i) #define each(a,b) for(auto&a:b) #define all(v) begin(v), end(v) #define RET(smth) return void(cout<<(smth)<<'\n'); #define sz(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; using Vi = vector<int>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int MM = 3013; int par[MM*MM]; int L[MM*MM]; int inv[MM*MM]; int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); } void Union(int a, int b) { a = find(a); b = find(b); if(a == b) return; if(par[a] > par[b]) swap(a,b); par[a] += par[b]; par[b] = a; L[a] += L[b]; } const int P = 1e9 + 7; int mul(int a, int b) { return 1LL*a*b%P; } int main() { #ifndef DEBUG cin.tie(0) -> sync_with_stdio(0); #endif int n,k; cin >> n >> k; fill(par,par+n*n,-1); inv[1] = 1; rep(i,2,n*n + 1) inv[i] = mul(P/i, P - inv[P%i]); rep(i,0,n) rep(j,i+1,n) L[i*n+j] = 1; Vi T(n); while(k--) { for(auto&v:T) cin >> v, --v; rep(i,0,n) rep(j,0,n) if(i!=j) Union(i*n+j,T[i]*n+T[j]); if(n != 1 && -par[find(1)] == n*(n-1)) break; } ll res = 0; rep(i,0,n) rep(j,0,n) if(i!=j) { int ind = i*n+j; if(par[ind] < 0) { int orb_size = -par[ind]; res += mul(inv[orb_size],mul(L[ind],orb_size-L[ind])); } } cout << res % P; return 0; } |