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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define  rep(i, n) for(int i = 0; i < n; i++) 
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  INF 1LL << 60
typedef long long int LL;
const int MOD = 1000000007;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }
//sort(all(x))とするとソートできるよ
// 10^x は pow(10,(x)) 
// 任意のlogは　対数の底の変換を使う    log(N) / log(任意の底)

// mod. m での a の逆元 a^{-1} を計算する
LL modinv(LL a, LL m) {
    LL b = m, u = 1, v = 0;
    while (b) {
        LL t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main(){
    LL N;cin >> N;
    LL K;cin >> K;
    vector<LL> A(N);
    rep(i,N)cin >> A[i];

    vector<LL> left(N,0);    //自分の左側に自分より小さいものがいくつあるか
    vector<LL> right(N,0);   //右側版


    rep(i,N){
        if(i == 0)left[0] = 0;
        for(int j = 0;j<i;j++){
            if(A[i] > A[j])left[i]++;
        }
    }
    rep(i,N){
        if(i == N-1)right[N-1] = 0;
        for(int j = N-1;j>i;j--){
            if(A[i]>A[j])right[i]++;
        }
    }
    


    vector<LL> S(N,0),r(N,0),l(N,0);
    
    LL ans = 0;
    LL R = 0,L=0;
    rep(i,N){
        R += right[i];
        L += left[i];
    }

    LL tmp1 = (R+L)%MOD;
    LL tmp2 = (K*(K+1))%MOD;
    LL tmp3 = tmp2 * modinv(2,MOD);
    LL tmp4 = L*K%MOD;
    ans = (tmp1 * tmp3) %MOD -tmp4; 

    if(ans < 0)ans += 1000000007;
    cout << ans%MOD << endl;

}