Inversions

3.3

3 votes
Medium-Hard, Addition Rules, Probablity, Expectation, Mathematics
Problem

You are given an array  of  integers and an integer . From the array , a new array  of size  is obtained by concatenating the array  times. For example, if  and , then .

In a step, one of the elements is randomly removed from . For example, if  then after one step, B could be  or  or . Let  be the expected value of the number of inversions in  after  steps. An inversion is defined as the number of pairs of indices  such that  and . Find the value of  modulo . It's guaranteed that the answer can be represented as rational fraction where , so print the value .

There are multiple test cases in a single input file.

Input format

  • First line: An integer  denoting the number of test cases.
  • First line of  test case: Two integers  and  followed by a line consisting of  integers of the array 

Output format

For each test case, print the answer in a new line.

Constraints

Sample Input
1
3 1
2 3 1
Sample Output
666666674
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

-

Editor Image

?