Editorial
Consider the cycle decomposition of . Since , we have . Thus every commutes with , and maps each cycle of only to another cycle of the same length. Hence the answer can be computed independently for each cycle length and multiplied.
Fix a length , and let be the number of cycles of length in . The action induced by the permutations on these cycles can be viewed as an action of . Therefore, each connected component contains exactly cycles for some .
Let be the number of ways to build one component on a fixed set of cycles.
If is odd, then
If is even, then is impossible, and for ,
Here
is the Gaussian binomial coefficient over . Since , it can be computed efficiently even for very large .
Let be the number of ways to process cycles of length . If the component containing the first remaining cycle has size , we choose the other cycles from the remaining cycles. Thus
Multiplying the values over all cycle lengths gives the answer. The total time complexity is .