Bermutation
Time limit2sMemory limit256 MB
Given a permutation and b, find the lexicographic rank of p among all permutations reachable by swapping the two halves of any 2b-length segment.
- Level
Hard9 of 10
- Topics
- Combinatorics, Math, Implementation
- Solved
- No attempts yet
Problem
You are given a permutation of integers from to . You may apply the following operation any number of times. Choose a segment of consecutive elements and swap its two halves. Formally, if you choose the segment , the result is .
Let be the set of all permutations that can be obtained from by applying this operation zero or more times. Each operation chooses its segment independently of the others. List all permutations in in lexicographical order, numbering them from . Find the position of in this list. Print the answer modulo .
Input
The first line contains one integer , the number of test cases. Each test case is given on two lines. The first line contains two integers and (, , ). The second line contains the elements of the permutation . Each integer from to appears exactly once.
The sum of over all test cases does not exceed .
Output
For each test case, print the 1-based position of in the lexicographically ordered list of all permutations obtainable from , modulo .