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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

public class S
{

    private const long DP_INIT_MINUS1 = -1;
    private const double DP_INIT_DOUBLE_MINUS1 = -1.0;
	private const long MOD = 1000000007;


    //s
    private const int FIRST_WIN = 1;
    private const int SECOND_WIN = 0;

    private static long[,] dp;
    private static long[] cSum;


    public static void Main()
	{
		/*
        var input = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
        var N = input[0];
        var M = input[1];
        */

        var input = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
        var N = input[0];
        var K = input[1];

        solver(int.Parse(N.ToString()),K,Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse));
    }

    public static void solver(int N, long K, int[] A){

        long count = 0;
        long selfCount = 0;
        long beforeCount = 0;

        long beforeMultiplier;

        int[] x = new int[2001];

        for(int i = 0; i < N; i++){
            selfCount += x[A[i]];
            for(int k = A[i]-1; k >= 1; k--){
                x[k]++;
            }
        }

        for(int i = 0; i < N; i++){
            beforeCount += x[A[i]];
        }

        count += selfCount * K;
        count %= MOD;

        if(K % 2 == 0){
            beforeMultiplier = K / 2;
            beforeMultiplier += (beforeMultiplier-1) * K;
        }else{
            beforeMultiplier = (K/2) * K;
        }

        beforeMultiplier %= MOD;

        //ここでlongが飛んでた
        count += beforeCount * beforeMultiplier;
        count %= MOD;


        // 自分自身で発生させる物と、後ろにあるならその数発生する物がある
        // 前者は固定で、全体数は*Kで求まる。後者は*1~k-1で求まる。

        Console.WriteLine(count % MOD);



    }

}