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;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int , int> i_i;
typedef pair<string , int> s_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> ve_vel;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<bool>> ve_veb;
typedef vector<vector<string>> ve_ves;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<(int)(n);i++)
#define rep2(i,n) for(int i=2;i<(int)(n);i++)
#define repk(i,k,n) for(int i=k;i<(int)(n);i++)
#define fs first
#define sc second
#define pub push_back
#define puf push_front
#define pob pop_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
//#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
const int64 INF = 1LL << 58;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<typename T> void remove(T & vector, unsigned int index){vector.erase(vector.begin() + index);}

ll bit[2005];
int N; ll K;

ll sum(int i) {
    ll s = 0;
    while(i > 0) {
        s += bit[i];
        i -= i & -i;
    }
    return s;
}

void add(int i, int x) {
    while(i <= N) {
        bit[i] += x;
        i += i & -i;
    }
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

    cin >> N >> K;
    vei A(N); rep(i,N) cin >> A[i];
    vei A2(2*N); rep(i,2*N) A2[i] = A[i%N];
    vei A3(3*N); rep(i,3*N) A3[i] = A[i%N];


    if(K <= 3){
        vei B(K*N); rep(i,K*N) B[i] = A[i%N];
        ll ansb = 0;
        rep(j, K*N) {
            ansb += (j - sum( B[j] )) % mod;
            add( B[j], 1 );
        }
        cout << ansb << endl;
        return 0;
    }
    memset(bit, 0LL, sizeof(bit));

    ll ans = 0LL;
    rep(j, N) {
        ans += (j - sum( A[j] )) % mod;
        add( A[j], 1 );
    }
    memset(bit, 0LL, sizeof(bit));

    ll ans2 = 0LL;
    rep(j, 2*N) {
        ans2 += (j - sum( A2[j] )) % mod;
        add( A2[j], 1 );
    }
    memset(bit, 0LL, sizeof(bit));

    ll ans3 = 0LL;
    rep(j, 3*N) {
        ans3 += (j - sum( A3[j] )) % mod;
        add( A3[j], 1 );
    }
    memset(bit, 0LL, sizeof(bit));
    
    ll tkaisa = (ans3 - ans2) - (ans2 - ans);
    ll kfirst = ans2 - ans;

    ll res = ans;

    ll re2 = (kfirst - tkaisa) * (K - 1);
    re2 %= mod;

    ll re3;
    if(K % 2 == 0) re3 = (K / 2) * (K - 1);
    else re3 = ( (K - 1) / 2 ) * K;
    re3 %= mod;

    re3 *= tkaisa;
    re3 %= mod;

    res += re2;
    res %= mod;
    res += re3;
    res %= mod;
    cout <<  res  << endl;
}