//With diy tree import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class FlagsIoana2 { private static int N,K,T; private static final long Prime = 1000000007; private static long nfact; public static class Node{ public Object[] children; public Node() { children = new Object[N+1]; for (int i = 0; i<=N; ++i) children[i] = null; } } public static class Tree{ public Node root; public ArrayList cycles; public ArrayList parents; public Tree(){ root = new Node(); cycles = new ArrayList(); parents = new ArrayList(); } public void addToCount(int[] cycle, long toadd) { int i = 1; Node crt = root; while (i<=N-1 ) { if (crt.children[cycle[i]] == null) crt.children[cycle[i]] = new Node(); crt = (Node)(crt.children[cycle[i]]); i++; } if (crt.children[cycle[N]] == null) { crt.children[cycle[N]] = new Long(toadd); cycles.add(cycle); parents.add(crt); } else //just add crt.children[cycle[N]] = new Long((toadd+((Long)crt.children[cycle[N]]).longValue())%Prime); } public Long get(int[] cycle) { Node crt = root; for (int i = 1; i 0) { if (e%2 == 1) fe = (fe*b) % Prime; b = (b * b) %Prime; e = e/2; } return fe; } private static void computeall() { //compute unnormalized counts int[] cycles = new int[N+1]; for (int i = 0; i<=N; ++i) cycles[i] = 0; cycles[1] = N; oldcounts = new Tree(); oldcounts.addToCount(cycles,(long)1); for (int i = 0; i=2) { combis = j * j * oldcycles[j] * (oldcycles[j]-1) / 2; totaladd = ( combis * oldcount) % Prime; cycles = new int[N+1]; for (int jj = 0; jj<=N; ++jj) cycles[jj] = oldcycles[jj]; cycles[j]-=2; cycles[2*j]+=1; newcounts.addToCount(cycles,totaladd % Prime ); } //swap among different-length cycles, it merges the cycles for (int other = j+1; other<=N; other++) if (oldcycles[other]!=0) { combis = j * other * oldcycles[j] * oldcycles[other] ; totaladd = ( combis * oldcount) % Prime; cycles = new int[N+1]; for (int jj = 0; jj<=N; ++jj) cycles[jj] = oldcycles[jj]; cycles[other]--; cycles[j]--; cycles[j+other]++; newcounts.addToCount(cycles,totaladd%Prime); } } } oldcounts = newcounts; } //normalize int oldsize = oldcounts.cycles.size(); for (int cc = 0; cc < oldsize; cc++) { int[] cycle = oldcounts.cycles.get(cc); long count = (Long)(oldcounts.parents.get(cc).children[cycle[N]]); for (int i = 1 ; i <=N ; ++i) if (cycle[i] > 0 ) { count = (count * fastexp(i,cycle[i])) %Prime; for (int j = 2; j<=cycle[i]; ++j) count = (count* j) % Prime; } count = (count * fastexp(nfact,Prime-2)) % Prime; oldcounts.parents.get(cc).children[cycle[N]]=new Long(count); } } private static void solve() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] line = br.readLine().split(" "); N = Integer.parseInt(line[0]); K = Integer.parseInt(line[1]); T = Integer.parseInt(line[2]); nfact = 1; for (int i = 2; i<=N;++i) nfact = (nfact*((long)i)) % Prime; computeall(); int[] perm = new int[N]; int[] cycleArray = new int[N+1]; boolean[] seen = new boolean[N]; for (int i = 0; i