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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
 
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define FOR(e, c) for (auto &e : c)
#define SORT(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define rvisort(v) sort(v.begin(), v.end(), greater<int>());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define rv(s) reverse(s.begin(),s.end());
 
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vs = vector<string>;
using vd = vector<double>;
using ul = unsigned long;
 
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int GCD(int a, int b) { return b ? GCD(b, a%b) : a; }

void printv(vi& v){
  rep(i,v.size()) cout << v[i] << " ";
  cout << endl;
}

class BIT {
private:
	ll N;
	vector<ll> node;
public:
	BIT(ll M) {
		node = vector<ll>(M + 1, 0);
		N = M;
	}
 
	ll sum(ll i) {
		ll s = 0;
		while (i > 0) {
			s += node[i];
			i -= i & -i;
		}
		return s;
	}
 
	ll sum(ll i, ll j) {
		ll l = sum(i - 1);
		ll r = sum(j);
		return r - l;
	}
 
	void add(ll i, ll x) {
		while (i <= N) {
			node[i] += x;
			i += i & -i;
		}
	}
};

int main()
{
  cin.tie( 0 );
  ios::sync_with_stdio( false );
  
  ll mod=1e9+7;
  
  ll n,k;cin>>n>>k;
  vi a(n);
  
  rep(i,n)cin>>a[i];
  
  ll x=0;
  
  BIT bit(n);
  for (int i = 0; i < n; i++){
    x += i - bit.sum(a[i]);
    bit.add(a[i], 1);
  }
  
  ll m=n*2;
  vi b(m);
  rep(i,m)b[i]=a[i%n];
  ll y=0;
  
  BIT bit2(m);
  for (int i = 0; i < m; i++){
    y += i - bit2.sum(b[i]);
    bit2.add(b[i], 1);
  }
  y=y-x*2;
  
  ll ans=((x*k)%mod+((k*(k-1)/2)%mod)*y%mod)%mod;
  cout(ans);
}