Sad Cuts

4

4 votes
Algorithms, Combinatorics, Fast Fourier transform, Hard
Problem

Are you sad ? Don't be, unlike me

You are given a sad array A of size N and an additional sad integer K. Now, we create a new sad array B, of size NK, where B[i]=A[i], for 0iN1 and B[i]=B[iN] for Ni(NK)1.

Now, we define an inversion to be a pair of indices (E1,E2) lying in the same subsequence S, such that E1<E2, but B[E1]>B[E2]

Now, you need to cut this sad array B into any number of sad non-empty non-intersecting subsequences. Each element from the given array should belong to exactly one of the subsequences. Let the array be cut into the set of subsequences {S0,S1,...Sk1}. Let Z be the sum of the number of inversions in each individual subsequence.

You know what's next right? Considering the array can be cut into each distinct set of subsequences with equal probability, you need to find the Expected Value of Z. What this means is that let the array be cut into the set of subsequences {S0,S1..Sk1} or into subsequences {C0,C1..Cl1}, where SC. The probability of array being in state S or in state C is equal to the probability of the array being cut into any other set of subsequences.

Let the answer be an irreducible fraction P/Q. You need to print PQ1 Modulo a given prime number M. It is guaranteed, that for all of the given tests, Q0 Modulo M. So, the answer will always exist and is unique for the given test-set, since M is prime.

Input Format:

The first line contains 3 space separated integers N, K and M. The next line contains N space-separated integers, where the ith integer denotes A[i].

Output Format:

Print the given answer on a single line

Constraints :

1N2000

1A[i]109

1K1010

3M5104

M is a prime number

Time Limit: 6
Memory Limit: 512
Source Limit:
Explanation

The array can be in any of the following final states, each with equal probability :

{(3),(2),(1)}

{(32),(1)}

{(3,1),(2)}

{(3),(2,1)}

{(3,2,1)}

So, E(Z)=15(0+1+1+1+3)=65=40 Modulo 97

 

Editor Image

?