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: Accepted)

                    /******************gourav_j**************/
#include <bits/stdc++.h>
#define FF first
#define SS second
#define pb push_back
#define mp make_pair
#define INF 1e18
#define MOD 1000000007
#define MAXN 100005
#define mem(a, v) memset(a, v, sizeof a)
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define fori(i,a,b) for(int  i=(a);i<=(b);i++)
#define forl(i,a,b) for(ll  i=(a);i<=(b);i++)
#define rfor(i,a,b) for(int i=(b);i>=(a);i--)
#define forit(i, a) for ( __typeof( (a).begin() ) i = (a).begin(); i != (a).end(); i++ )
#define all(v) (v.begin(),v.end())
using namespace std;
 
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
typedef map<string,ll> msi;
typedef set<ll> si;
int dx[] = {-1, 0, 0, 1};
int dy[] = { 0,-1, 1, 0};
                /***********code begins here***********/
int getInvCount(ll arr[],int n) { 
    multiset<ll> set1; 
    set1.insert(arr[0]); 
    int invcount = 0;
    multiset<ll>::iterator itset1;
    for (int i=1; i<n; i++) 
    { 
        set1.insert(arr[i]); 
        itset1 = set1.upper_bound(arr[i]); 
        invcount += distance(itset1, set1.end()); 
    }  
    return invcount; 
}
ll gcdExtended(ll a, ll b, ll *x, ll *y) { 
    if (a == 0) 
    { 
        *x = 0, *y = 1; 
        return b; 
    } 
  
    ll x1, y1; 
    ll gcd = gcdExtended(b%a, a, &x1, &y1); 
    *x = y1 - (b/a) * x1; 
    *y = x1; 
    return gcd; 
} 
ll modInverse(ll b) { 
    ll x, y;
    ll g = gcdExtended(b, MOD, &x, &y); 
    if (g != 1) 
        return -1; 
    return (x%MOD + MOD) % MOD; 
} 
void solve(){
    int n;
    ll k;
    cin>>n>>k;
    ll ar[n];
    fori(i,0,n-1)
    cin>>ar[i];
    int inv = getInvCount(ar,n);
    ll ans = (k%MOD*inv%MOD)%MOD;
    for(int i=0;i<n;i++){
        ll c=0;
        for(int j=0;j<n;j++){
            if(ar[i]>ar[j])
                c++;
        }
        ll y = (k-1)%MOD;
        ll x = (c+((k-1)%MOD*c%MOD)%MOD)%MOD;
        x = (x*y)%MOD;
        ll z = modInverse(2);
        x = (x*z)%MOD;
        ans = (ans+x)%MOD;
    }
    /*for(int i=0;i<n;i++){
        ll y = (k-1);
        ll x = (br[i]+((k-1)*br[i])%MOD)%MOD;
        ans = (ans+((x*y)/2)%MOD)%MOD;
    }*/
    cout<<ans;
}
int main() {
    
    int T=1; 
    //cin>>T;
    while(T--){
        solve();
        cout<<'\n';
    }
    return 0;
}