2759: 3602 Counting Swaps
Description
Just like yesterday (in problem U of the practice session), Bob is busy, so Alice keeps on playing some single-pla
Instead of simply solving the puzzle, Alice is wondering about the probability of winning it just by playing at random. In order to answer this question, she needs to know the number of optimal solutions to her puzzle.
You are given a permutation p1, …, pn of the numbers 1 through n. In each step you can choose two numbers x < y and swap px with py.
Let m be the minimum number of such swaps needed to sort the given permutation. Compute the number of different sequences of exactly m swaps that sort the given permutation. Since this number may be large, compute it modulo 109 + 9.
描述
Input
The first line of the input file contains an integer t specifying the number of test cases. Each test case is preceded by a blank line.
Each test case consists of two lines. The first line contains the integer n. The second line contains the sequence p1, …, pn: a permutation of 1, …, n.
In the easy subproblem C1, 1 ≤ n ≤ 10.
In the hard subproblem C2, 1 ≤ n ≤ 105.
Output
Sample Input Copy
样例输入
3
3
2 3 1
4
2 1 4 3
2
1 2
Sample Output Copy
3
2
1