Problem:
Score : 300 points

Problem Statement

We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.

Let B be a sequence of K \times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

Find the inversion number of B, modulo 10^9 + 7.

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \leq i < j \leq K \times N - 1) such that B_i > B_j.

Constraints

All values in input are integers.

1 \leq N \leq 2000

1 \leq K \leq 10^9

1 \leq A_i \leq 2000

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 ... A_{N - 1}

Output

Print the inversion number of B, modulo 10^9 + 7.

Sample Input 1

2 2
2 1

Sample Output 1

3

In this case, B~=~2,~1,~2,~1. We have:

B_0 > B_1

B_0 > B_3

B_2 > B_3

Thus, the inversion number of B is 3.

Sample Input 2

3 5
1 1 1

Sample Output 2

0

A may contain multiple occurrences of the same number.

Sample Input 3

10 998244353
10 9 8 7 5 6 3 4 2 1

Sample Output 3

185297239

Be sure to print the output modulo 10^9 + 7.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main {

	final static long MOD = 1000000007;
	public static String[] Ws = null;
	public static int wsIndx = 0;
	public static BufferedReader in = null;
	public static void main(String[] args) throws Exception {

		int N = nextInt();
		long K = nextInt();
		int[] A = nextInts();
		int[] g = new int[N];
		int[] l = new int[N];
		Arrays.fill(g, 0);
		Arrays.fill(l, 0);
		for(int i=0;i<N;i++){
			for(int k=0;k<N;k++){
				if(A[i]>A[k]){
					g[k]++;
				}
				if(A[i]>A[k]&&i<k){
					l[k]++;
				}
			}
		}

		long ans =0;
		for(int i=0;i<N;i++){
			ans+=((K-1)*(K))%MOD*g[i]/2%MOD;
		}
		for(int i=0;i<N;i++){
			ans+=K*l[i];
			ans%=MOD;
		}


		System.out.println(ans);

//		test(new int[]{1,2});
//		test(new int[]{2,1});
	}
	public static void test(int[] A){
		System.out.println(Arrays.toString(A));
		int[] x = new int[10];
		for(int K=1;K<10;K++){
			int N=A.length;
			int[] B = new int[A.length*K];
			for(int i=0;i<N;i++){
				for(int k=0;k<K;k++){
					B[k*N+i]=A[i];
				}
			}

			int cnt=0;
			for(int i=0;i<K*N;i++){
				for(int k=i+1;k<K*N;k++){
					if(B[i]>B[k]){
						cnt++;
					}
				}
			}
			System.out.println("K="+K+" :"+cnt);
			x[K]=cnt;
		}
		x[0]=0;
		for(int i=1;i<x.length;i++){
			System.out.println(x[i]-x[i-1]);
		}
	}

	public static String[] toStringArray(String s) {
		String[] tmp = new String[s.length()];
		char[] c = s.toCharArray();
		for (int i = 0; i < tmp.length; i++) {
			tmp[i] = String.valueOf(c[i]);
		}
		return tmp;
	}

	public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	}
	public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	}

	public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	}

	public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	}

	public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	}


}