#include #include #include #include #include #include using namespace std; #define MOD 1000000007 #define MULT(x,y) ((((long long)(x)) * ((long long)(y)))%(MOD)) #define ADD(x,y) ((((long long)(x)) + ((long long)(y)))%(MOD)) map,int> solve(int n, int k) { map,int> res, aux; vector id(n+1); id[1] = n; res[id] = 1; // each partition is described by how many parts it has of each size // start from identity : identity has n parts of size 1 // The the process is reversible (we can do the steps backwards), and we work from the identity // the numbers we obtain for each partition must then be normalized by the number of permutations // as all of them contribute equally to the sum for (int t = 0; t < k; t++) { aux.clear(); for(auto it = res.begin(); it != res.end(); it++) { vector partition = it->first; int cnt = it->second; for (int i = 1; i <= n; i++) { if (partition[i] > 0) { for (int j = i+1; j<=n; j++) { if (partition[j] > 0) { int c = MULT(MULT(MULT(MULT(partition[i],partition[j]),i),j),cnt); partition[i]--;partition[j]--;partition[i+j]++; aux[partition] = ADD(aux[partition],c); partition[i]++;partition[j]++;partition[i+j]--; } } for (int j = 1; j<(i+1)/2; j++) { // the half case is complicated int c = MULT(MULT(partition[i],i),cnt); partition[i]--;partition[j]++;partition[i-j]++; aux[partition] = ADD(aux[partition],c); partition[i]++;partition[j]--;partition[i-j]--; } if ((i&1) == 0) { // the half case is complicated int i2 = i>>1; int c = MULT(MULT(partition[i],i2),cnt); partition[i]--;partition[i2]+=2; aux[partition] = ADD(aux[partition],c); partition[i]++;partition[i2]-=2; } if (partition[i] > 1) { int comb = (partition[i]*(partition[i]-1))/2; int c = MULT(i,MULT(i,MULT(cnt,comb))); partition[i]-=2;partition[2*i]++; aux[partition] = ADD(aux[partition],c); partition[i]+=2;partition[2*i]--; } } } } res = aux; } return res; } vector vector_to_partition(vector v) { int n = v.size(); vector cyc(n+1,0); // cycle structure vector vis(n,false); // visited ? for (int i = 0; i 0) { if (m&1) { res = MULT(res,a); } a = MULT(a,a); m = m >> 1; } return res; } // easy modular inverse for m < MOD. int modular_inverse(int m) { return modular_power(m,MOD-2); } int factorial(int m) { int ans = 1; for (int i = 2; i <= m; i++) ans = MULT(ans,i); return ans; } // number of permutations for a given partition of n int number_of_permutations(int n, vector p) { int ans = factorial(n); for (int i = 1 ; i < p.size(); i++) { if (p[i] > 0) { int pf = modular_power(i,p[i]); int pf2 = MULT(pf,factorial(p[i])); ans = MULT(ans,modular_inverse(pf2)); } } return ans; } int main(void) { int n,k,T,val; scanf("%d %d %d",&n,&k,&T); auto unnormalized = solve(n,k); // presolve for every possible partition vector v(n,0); for (int a = 0; a< T; a++) { for (int i = 0; i < n; i++) { scanf("%d",&val); v[i] = val-1; } vector partition = vector_to_partition(v); int normalize = modular_inverse(number_of_permutations(n,partition)); printf("%d\n",(int)MULT(unnormalized[partition],normalize)); } return 0; }