Under construction

Still being built. Things may change

Bermutation

Time limit2sMemory limit256 MB

Summary
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 pp of integers from 11 to nn. You may apply the following operation any number of times. Choose a segment of 2b2b consecutive elements and swap its two halves. Formally, if you choose the segment ai,ai+1,…,ai+2b−1a_i, a_{i+1}, \ldots, a_{i+2b-1}, the result is ai+b,ai+b+1,…,ai+2b−1,ai,ai+1,…,ai+b−1a_{i+b}, a_{i+b+1}, \ldots, a_{i+2b-1}, a_i, a_{i+1}, \ldots, a_{i+b-1}.

Let SS be the set of all permutations that can be obtained from pp by applying this operation zero or more times. Each operation chooses its segment independently of the others. List all permutations in SS in lexicographical order, numbering them from 11. Find the position of pp in this list. Print the answer modulo 120 586 241120\,586\,241.

Input

The first line contains one integer TT, the number of test cases. Each test case is given on two lines. The first line contains two integers nn and bb (2≤n≤1052 \le n \le 10^5, 1≤b1 \le b, 2b≤n2b \le n). The second line contains the nn elements of the permutation pp. Each integer from 11 to nn appears exactly once.

The sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, print the 1-based position of pp in the lexicographically ordered list of all permutations obtainable from pp, modulo 120 586 241120\,586\,241.

Examples1

  1. Example 1

    Input
    2
    5 1
    2 3 1 4 5
    5 2
    2 3 1 4 5
    
    Expected output
    31
    3