Inversions

3.3

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

You are given an array A of n integers and an integer k. From the array A, a new array B of size n×k is obtained by concatenating the array Ak times. For example, if A=[2,3,2] and k=2, then B=[2,3,2,2,3,2].

In a step, one of the elements is randomly removed from B. For example, if B=[2,3,2] then after one step, B could be [2,3] or [2,2] or [3,2]. Let E[i] be the expected value of the number of inversions in B after i steps. An inversion is defined as the number of pairs of indices i,j such that i<j and B[i]>B[j]. Find the value of n×ki=0 E[i] modulo 109+7. It's guaranteed that the answer can be represented as rational fraction PQ where gcd(P,Q)=1 , so print the value P.Q1mod109+7.

There are multiple test cases in a single input file.

Input format

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

Output format

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

Constraints

1t105

1ti=1 ni3105

1ni×ki109

1A[i]n

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

-

Editor Image

?