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)

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <list>
#include <numeric>
#include <stack>
#include <iomanip>

using namespace std;

#define Rep(i,a,b) for(int i = a; i < b; i++)
#define rep(i,b) Rep(i,0,b)
#define allof(a) (a).begin(), (a).end()

typedef long long ll;
const int inf = 1e9 + 7;
const ll infll = 1ll << 60ll;
const ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

namespace {  // 名前なし名前空間
	// gcd, lcm は C++17 でstdに標準入りしたので言語のバージョンに注意
	// 最大公約数
	ll gcd(ll a, ll b) {
		if (b == 0) return a;
		return gcd(b, a % b);
	}

	// 最小公倍数
	ll lcm(ll a, ll b) {
		return a / gcd(a, b) * b;
	}

	void dfs(int n, int i, int& ans, const vector<ll>& num , const vector<vector<ll>>& numIndex) {

	}
}

int main() {

	ll n, k;
	cin >> n >> k;
	vector<ll> a(n);
	rep(i, n) cin >> a[i];

	// O(n^2)行ける
	//vector<ll> num(2001, 0);
	//rep(i, n) {
	//	num[a[i]]++;
	//}
	//cout << "a" << endl;

	//vector<vector<ll>> numIndex(n + 1, vector<ll>(2001, 0));
	//Rep(i, 1, n + 1) {
	//	Rep(j, 1, 2001) {
	//		if (j == a[i])
	//			numIndex[i][j] = numIndex[i - 1][j] + 1;
	//		else {
	//			numIndex[i][j] = numIndex[i - 1][j];
	//		}
	//	}
	//}

	ll ans = 0;
	// 左から見ていく
	// n
	rep(i, n) {
		// 数字a[i]をみて計算する
		// a[i]より小さい数
		ll sum = 0;
		Rep(j, i + 1, n) {
			if (a[i] > a[j])sum++;
		}
		ll sumAll = 0;
		rep(j, n) {
			if (a[i] > a[j])sumAll++;
		}
		ans += sum * k;
		ans %= mod;
		// ここでオーバーフローして層
		//ans += ((k - 1) * k / 2) * sumAll;
		ll add = 0;
		add = k * (k - 1) / 2;
		add %= mod;
		add *= sumAll;
		add %= mod;
		ans += add;
		ans %= mod;
	}

	cout << ans << endl;

	return 0;
}