stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=2005,mod=1e9+7;

int a[N];

int c1[N],c2[N];

int main(){

    int n,k;

    ll n2=500000004;

    scanf("%d %d",&n,&k);

    for(int i=0;i<n;i++) scanf("%d",&a[i]);

    for(int i=0;i<n;i++){

        int num=0;

        for(int j=0;j<i;j++){

            if(a[i]<a[j]) num++;

        }

        c1[i]=num;

        for(int j=i+1;j<n;j++){

            if(a[i]<a[j]) num++;

        }

        c2[i]=num;

    }

    ll ans=0;

    for(int i=0;i<n;i++){

            ans+=1ll*k*c1[i]%mod;

            ans%=mod;

            ans+=1ll*c2[i]*(k-1)%mod*k%mod*n2%mod;

            ans%=mod;

    }

    printf("%lld",ans);

}


// Faster C++ solution for the same problem (measured speedup: 5.1236236342306345x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

typedef long long ll;

const int maxn=2e5+100;

const int mod=1e9+7;

int a[maxn];

int l[maxn];//记录左边比i要小的数 的个数 

int r[maxn];//记录右边比i要小的数 的个数

int main()

{

	int n,k;

	ll ans;

	ll sam;

	scanf("%d%d",&n,&k); 

	sam=(ll)(k+1)*k/2;//总和时要算右边比i小的次数 

	sam-=k; //总和时要算左边比i小的次数 

	sam=(sam+mod)%mod;

	for(int i=0;i<n;i++)

		scanf("%d",&a[i]);

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<i;j++)

			if(a[j]<a[i])

				l[i]++;

		for(int j=i+1;j<n;j++)

			if(a[j]<a[i])

				r[i]++;		 

	}

	ans=0; 

	for(int i=0;i<n;i++)

	{

		ans+=(l[i]*sam);

		ans%=mod;

		ans+=(r[i]*(sam+k));

		ans%=mod;

	}

	printf("%lld\n",ans);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll mod=1e9+7;

const ll N=1e5;

ll dp[N];

ll dp1[N];

ll arr[N];

ll n,k;

ll ksm(ll a, ll b){

	ll sum =1;

	while(b){

		if(b&1)

			sum = sum * a % mod;

		a=a*a%mod;

		b>>=1;

	}

	return sum;

}





ll solve(ll a){

	ll sum=((k*k+k)/2)%mod;

	sum=(dp[a]*sum)%mod;

	ll sum1=(k*(k-1)/2)%mod;

	sum1=(dp1[a]*sum1)%mod;

	sum=(sum1+sum+mod)%mod;

	return sum;

}



int main(){

	ll ans=0;

	scanf("%lld%lld",&n,&k);

	for(ll i=1;i<=n;i++){

		scanf("%lld",arr+i);

	} 

	for(ll i=1;i<=n;i++){

		for(int k=1;k<=i;k++){

			if(arr[i]>arr[k])

				dp1[i]++;

		}

		for(ll j=i+1;j<=n;j++){

			if(arr[i]>arr[j])

				dp[i]++;

		}

	} 

	for(ll i=1;i<=n;i++){

		ans=(ans+solve(i)+mod)%mod;

	}

	printf("%lld\n",(ans+mod)%mod);

	

	

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.122952783984547x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

typedef long long ll;

const int maxn=2e5+100;

const int mod=1e9+7;

int a[maxn];

int l[maxn];//记录左边比i要小的数 的个数 

int r[maxn];//记录右边比i要小的数 的个数

int main()

{

	int n,k;

	ll ans;

	ll sam;

	scanf("%d%d",&n,&k); 

	sam=(ll)(k+1)*k/2;//总和时要算右边比i小的次数 

	sam-=k; //总和时要算左边比i小的次数 

	sam=(sam+mod)%mod;

	for(int i=0;i<n;i++)

		scanf("%d",&a[i]);

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<i;j++)

			if(a[j]<a[i])

				l[i]++;

		for(int j=i+1;j<n;j++)

			if(a[j]<a[i])

				r[i]++;		 

	}

	ans=0; 

	for(int i=0;i<n;i++)

	{

		ans+=(l[i]*sam);

		ans%=mod;

		ans+=(r[i]*(sam+k));

		ans%=mod;

	}

	printf("%lld\n",ans);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll mod=1e9+7;

ll a[2222];

ll b[2222];

int main()

{

	//ios::sync_with_stdio(flase);

	ll n,k;

	cin>>n>>k;

	for(int i=0;i<n;i++)

	{

		scanf("%lld",&a[i]);

	}

	

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<n;j++)

		{

			if(a[i]>a[j])

				b[i]++;

		}

	}

	ll sum=0;

	ll cnt=0;

	cnt=k%mod*((k-1)%mod)/2%mod;



		

	for(int i=0;i<n;i++)

	{

		sum=sum+(b[i]%mod)*((cnt)%mod)%mod;

	}

	ll sum1=0;

	for(int i=0;i<n;i++)

	{

		for(int j=i+1;j<n;j++)

		{

			if(a[i]>a[j])

				sum1++;

		}

	}

	sum1=k%mod*(sum1%mod)%mod;

	sum=(sum+sum1)%mod;

	cout<<sum%mod<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.20091967707381x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

typedef long long ll;

const int maxn=2e5+100;

const int mod=1e9+7;

int a[maxn];

int l[maxn];//记录左边比i要小的数 的个数 

int r[maxn];//记录右边比i要小的数 的个数

int main()

{

	int n,k;

	ll ans;

	ll sam;

	scanf("%d%d",&n,&k); 

	sam=(ll)(k+1)*k/2;//总和时要算右边比i小的次数 

	sam-=k; //总和时要算左边比i小的次数 

	sam=(sam+mod)%mod;

	for(int i=0;i<n;i++)

		scanf("%d",&a[i]);

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<i;j++)

			if(a[j]<a[i])

				l[i]++;

		for(int j=i+1;j<n;j++)

			if(a[j]<a[i])

				r[i]++;		 

	}

	ans=0; 

	for(int i=0;i<n;i++)

	{

		ans+=(l[i]*sam);

		ans%=mod;

		ans+=(r[i]*(sam+k));

		ans%=mod;

	}

	printf("%lld\n",ans);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstring>



using namespace std;

typedef long long ll; 

const ll N = 5005;

const ll mod = 1e9+7;  

ll n,k;

ll f[N],h[N],a[N],sum[N];



int main(){

	memset(sum,0,sizeof(sum));

	memset(f,0,sizeof(f));

	memset(h,0,sizeof(h));

	memset(a,0,sizeof(a));

	cin>>n>>k;

	for(ll i=1;i<=n;i++) cin>>a[i];

	for(ll i=1;i<=n;i++){

		for(ll j=i+1;j<=n;j++){

			if(a[i]>a[j]) h[i]++;

		}

		for(ll j=1;j<i;j++){

			if(a[i]>a[j]) f[i]++;

		} 

		sum[i]=f[i]+h[i];

	}

	ll ans=0;

	for(ll i=1;i<=n;i++){

		ans=((ans+(sum[i]*((k-1)*k/2%mod)%mod)%mod)%mod+h[i]*k)%mod;

	}

	cout<<ans%mod<<endl;

	

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 1.157932757313351x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;

 

const ll mod = 1e9+7;

 

 

int num[2000+10];

int a[2000+10];

int b[2000+10];

 

int main()

{

	ll n,k;

	cin>>n>>k;

	for(int i=0; i<n; i++)

		cin>>num[i];

	for(int i=0; i<n; i++)

	{

		int j=i+1;

		while(j < n) 

		{

			if(num[j] < num[i]) a[i]++;	

			j++;		

		}

		if(!b[num[i]])

			{

				for(int k=0; k<n; k++)

				{

					if(num[k] < num[i])

					b[num[i]]++;

				}

			}

	}

//	for(int i=0; i<n; i++) cout<<a[i]<<' '<<b[num[i]]<<endl;

 

	ll ans=0;

	ll sum=(k-1)*k/2;//放在下面的求ans语句中就会wa,求逆元也不行,遇到除法还是单独运算比较好.

	sum = sum % mod;

	for(int i=0; i<n; i++)

	{

//		ans += b[num[i]]*sum%mod;

//		ans += k*a[i]%mod;

		

		ans=(ans+k*a[i]%mod+b[num[i]]*sum%mod)%mod;

	}

	cout<<ans%mod;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstring>



using namespace std;

typedef long long ll; 

const ll N = 5005;

const ll mod = 1e9+7;  

ll n,k;

ll f[N],h[N],a[N],sum[N];



int main(){

	memset(sum,0,sizeof(sum));

	memset(f,0,sizeof(f));

	memset(h,0,sizeof(h));

	memset(a,0,sizeof(a));

	cin>>n>>k;

	for(ll i=1;i<=n;i++) cin>>a[i];

	for(ll i=1;i<=n;i++){

		for(ll j=i+1;j<=n;j++){

			if(a[i]>a[j]) h[i]++;

		}

		for(ll j=1;j<i;j++){

			if(a[i]>a[j]) f[i]++;

		} 

		sum[i]=f[i]+h[i];

	}

	ll ans=0;

	for(ll i=1;i<=n;i++){

		ans=((ans+(sum[i]*((k-1)*k/2%mod)%mod)%mod)%mod+h[i]*k)%mod;

	}

	cout<<ans%mod<<endl;

	

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 1.1554952529553288x):
#include<bits/stdc++.h>

#include<iostream>

#include<cmath>

#define rep(i,a,b) for(int i=a;i<=b;i++)

typedef long long ll;

using namespace std;

const int inf=0x3f3f3f3f;

const ll INF=0x3f3f3f3f3f3f3f3f;

const double eps = 0.0000001;

const int N=2200;

const int M=1e5+10;

const int mod=1e9+7;

void io(){

	ios::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

}

int aa[2010];

ll p(ll a,ll b,ll mod)

{

    ll ans=0;

    for(;b;b>>=1)

    {

        if(b&1)

        {

            ans=ans+a%mod;

        }

        a=a*2%mod;

    }

    return ans%mod;

}

int main()

{

   int n;

   ll k;

   cin>>n>>k;

   for(int i=1;i<=n;i++)

       cin>>aa[i];

   int sum=0;

   for(int i=1;i<=n;i++)

       for(int j=1;j<=n;j++)

           if(aa[i]>aa[j]) sum++;

   int summ=0;

   for(int i=1;i+1<=n;i++)

       for(int j=i+1;j<=n;j++)

       if(aa[j]<aa[i]) summ++;

    ll ans=(ll)p((ll)summ,k,mod);

    ans+=p(k*(k-1)/2,(ll)sum,mod)%mod;

    cout<<ans%mod<<endl;

}
// Slower C++ solution:
#include<iostream>

#include<cstring>



using namespace std;

typedef long long ll; 

const ll N = 5005;

const ll mod = 1e9+7;  

ll n,k;

ll f[N],h[N],a[N],sum[N];



int main(){

	memset(sum,0,sizeof(sum));

	memset(f,0,sizeof(f));

	memset(h,0,sizeof(h));

	memset(a,0,sizeof(a));

	cin>>n>>k;

	for(ll i=1;i<=n;i++) cin>>a[i];

	for(ll i=1;i<=n;i++){

		for(ll j=i+1;j<=n;j++){

			if(a[i]>a[j]) h[i]++;

		}

		for(ll j=1;j<i;j++){

			if(a[i]>a[j]) f[i]++;

		} 

		sum[i]=f[i]+h[i];

	}

	ll ans=0;

	for(ll i=1;i<=n;i++){

		ans=((ans+(sum[i]*((k-1)*k/2%mod)%mod)%mod)%mod+h[i]*k)%mod;

	}

	cout<<ans%mod<<endl;

	

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 1.1688675102298414x):
#include <bits/stdc++.h>



using namespace std;

typedef unsigned long long ll;

const int mod=1e9+7;

int a[20010];

int main()

{

	ll n,k;

	scanf("%lld %lld",&n,&k);

	ll cot=0; 

	for(int i=0;i<n;i++)

	{

		scanf("%d",&a[i]);

	}

	for(int i=0;i<n;i++)

	{

		for(int j=i+1;j<n;j++)

		{

			if(a[i]>a[j]) cot++;

		}

	}

	ll cnt=0;

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<i;j++)

		{

			if(a[j]<a[i]) cnt++;

		}

	} 

	ll sum=0;

	ll kk=k*(k+1)/2;

	if(kk==1) cout <<cot;

	else

	{

		ll kkk=(k-1)*(k)/2;

		sum=cot%mod*(kk%mod)%mod+cnt%mod*(kkk%mod)%mod;

		cout <<sum%mod<<endl;

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>



using namespace std;

typedef long long ll;

const ll mod = 1e9 + 7;

const int N = 10000;



ll a[N]; 

ll r[N],l[N];

int main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);cout.tie(0);

	ll n,k,ans;

	cin>>n>>k;

	for(ll i=1;i<=n;i++) cin>>a[i];

	ll num=0;

	num=(k+1)*k/2;

	num-=k;

	num%=mod;

	ans=0;

	memset(r,0,sizeof(r));

	memset(l,0,sizeof(l));

	for(ll i=1;i<=n;i++)

	{

		for(ll j=1;j<i;j++)

		{

			if(a[i]>a[j])

				l[i]++;//这个数左边小于当前位置的数的个数 

		}

		for(ll j=i+1;j<=n;j++)

		{

			if(a[i]>a[j])

				r[i]++;//这个数右边小于当前位置的数的个数 

		}

	}

//	cout<<num<<endl;

	for(ll i=1;i<=n;i++)

	{

//		cout<<l[i]<<' '<<r[i]<<endl;

		ans=ans+l[i]*num%mod;

		ans=ans+r[i]*(num+k)%mod;

	}

	cout<<ans%mod<<endl;	

	return 0;

}






// Faster C++ solution for the same problem (measured speedup: 1.1814270337560207x):
#include <bits/stdc++.h>



using namespace std;

typedef unsigned long long ll;

const int mod=1e9+7;

int a[20010];

int main()

{

	ll n,k;

	scanf("%lld %lld",&n,&k);

	ll cot=0; 

	for(int i=0;i<n;i++)

	{

		scanf("%d",&a[i]);

	}

	for(int i=0;i<n;i++)

	{

		for(int j=i+1;j<n;j++)

		{

			if(a[i]>a[j]) cot++;

		}

	}

	ll cnt=0;

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<i;j++)

		{

			if(a[j]<a[i]) cnt++;

		}

	} 

	ll sum=0;

	ll kk=k*(k+1)/2;

	if(kk==1) cout <<cot;

	else

	{

		ll kkk=(k-1)*(k)/2;

		sum=cot%mod*(kk%mod)%mod+cnt%mod*(kkk%mod)%mod;

		cout <<sum%mod<<endl;

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 2e4 + 10;



ll a[MAXN], gall[MAXN], gback[MAXN];



int main() {

	ios::sync_with_stdio(false);

	ll n, k, mod = 1e9 + 7;

	while(cin >> n >> k) {

		memset(gall, 0, sizeof(gall));

		memset(gback, 0, sizeof(gback));

		for(int i = 0; i < n; i++) {

			cin >> a[i];

		}

		for(int i = 0; i < n; i++) {

			for(int j = 0; j < n; j++) {

				if(a[i] > a[j]) {

					gall[i]++;

					if(j > i) {

						gback[i]++;

					}

				}

			}

		}

		ll ans = 0;

		for(int i = 0; i < n; i++) {

			ans = (ans + k * gback[i] + (k * (k - 1) / 2 % mod) * gall[i] % mod) % mod;

		}

		cout << ans % mod << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.327389263600621x):
#include<iostream>

using namespace std;

int a[12345];

int flag[12345];

long long mod=1e9+7;

int main()

{

	long long n,k,sum1=0;

	cin>>n>>k;

	k=k%mod;

	for(int i=0;i<n;i++)

	{

		cin>>a[i];

		for(int j=0;j<i;j++)

			if(a[j]<a[i])

				flag[i]++;

	}

	

	for(int i=0;i<n-1;i++)

	{

		for(int j=i+1;j<n;j++)

		if(a[i]>a[j])

			sum1++;

	}

	long long t=(k+k*(k-1)/2)%mod;

	sum1=sum1*t%mod;//section one

	

	long long sum2=0;

	for(int i=0;i<n;i++)

	{

		sum2+=flag[i];

	}

	t=((k-1)+(k-1)*(k-2)/2)%mod;

	sum2=sum2*t%mod;//scetion two

	

	long long sum=(sum1+sum2)%mod;

	cout<<sum%mod<<endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 2e4 + 10;



ll a[MAXN], gall[MAXN], gback[MAXN];



int main() {

	ios::sync_with_stdio(false);

	ll n, k, mod = 1e9 + 7;

	while(cin >> n >> k) {

		memset(gall, 0, sizeof(gall));

		memset(gback, 0, sizeof(gback));

		for(int i = 0; i < n; i++) {

			cin >> a[i];

		}

		for(int i = 0; i < n; i++) {

			for(int j = 0; j < n; j++) {

				if(a[i] > a[j]) {

					gall[i]++;

					if(j > i) {

						gback[i]++;

					}

				}

			}

		}

		ll ans = 0;

		for(int i = 0; i < n; i++) {

			ans = (ans + k * gback[i] + (k * (k - 1) / 2 % mod) * gall[i] % mod) % mod;

		}

		cout << ans % mod << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3335977739040328x):
#include<bits/stdc++.h>



using namespace std;



typedef long long ll;

const ll mod=1e9+7;

ll a[5000];

int main()

{

	ll n,k;

	ll ans=0;

	ll temp=0;

	cin>>n>>k;

	for(int i=0;i<n;i++){

		cin>>a[i];

	}

	for(int i=n;i<2*n;i++){

		a[i]=a[i-n];

	}

//	cout<<n;

	for(int i=0;i<n;i++){

		for(int j=n;j<2*n;j++){

			if(a[i]>a[j])ans++;

		}

//		cout<<ans<<" ";

		for(int k=i+1;k<n;k++){

//			if(k==n) continue;

			if(a[i]>a[k]) temp++;

		}

	}

//	cout<<ans<<" "<<temp<<endl;

	cout<<(((k*(k-1)/2)%mod*ans%mod)%mod+(temp*k)%mod)%mod; 

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

typedef long long ll;

const int maxn=2e5+100;

const int mod=1e9+7;

int a[maxn];

int l[maxn];//记录左边比i要小的数 的个数 

int r[maxn];//记录右边比i要小的数 的个数

int main()

{

	int n,k;

	ll ans;

	ll sam;

	int a[maxn];

	int l[maxn];//记录左边比i要小的数 的个数 

	int r[maxn];//记录右边比i要小的数 的个数

	memset(l,0,sizeof(l));

	memset(r,0,sizeof(r));

	scanf("%d%d",&n,&k); 

	sam=(ll)(k+1)*k/2;//总和时要算右边比i小的次数 

	sam-=k; //总和时要算左边比i小的次数 

	sam=(sam+mod)%mod;

	for(int i=0;i<n;i++)

		scanf("%d",&a[i]);

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<i;j++)

			if(a[j]<a[i])

				l[i]++;

		for(int j=i+1;j<n;j++)

			if(a[j]<a[i])

				r[i]++;		 

	}

	ans=0; 

	for(int i=0;i<n;i++)

	{

		ans+=(l[i]*sam);

		ans%=mod;

		ans+=(r[i]*(sam+k));

		ans%=mod;

	}

	printf("%lld\n",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8367078877676453x):
#include<bits/stdc++.h>



using namespace std;



typedef long long ll;

const ll mod=1e9+7;

ll a[5000];

int main()

{

	ll n,k;

	ll ans=0;

	ll temp=0;

	cin>>n>>k;

	for(int i=0;i<n;i++){

		cin>>a[i];

	}

	for(int i=n;i<2*n;i++){

		a[i]=a[i-n];

	}

//	cout<<n;

	for(int i=0;i<n;i++){

		for(int j=n;j<2*n;j++){

			if(a[i]>a[j])ans++;

		}

//		cout<<ans<<" ";

		for(int k=i+1;k<n;k++){

//			if(k==n) continue;

			if(a[i]>a[k]) temp++;

		}

	}

//	cout<<ans<<" "<<temp<<endl;

	cout<<(((k*(k-1)/2)%mod*ans%mod)%mod+(temp*k)%mod)%mod; 

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 2e4 + 10;



ll a[MAXN], gall[MAXN], gback[MAXN];



int main() {

	ios::sync_with_stdio(false);

	ll n, k, mod = 1e9 + 7;

	while(cin >> n >> k) {

		memset(gall, 0, sizeof(gall));

		memset(gback, 0, sizeof(gback));

		for(int i = 0; i < n; i++) {

			cin >> a[i];

		}

		for(int i = 0; i < n; i++) {

			for(int j = 0; j < n; j++) {

				if(a[i] > a[j]) {

					gall[i]++;

					if(j > i) {

						gback[i]++;

					}

				}

			}

		}

		ll ans = 0;

		for(int i = 0; i < n; i++) {

			ans = (ans + k * gback[i] + (k * (k - 1) / 2 % mod) * gall[i] % mod) % mod;

		}

		cout << ans % mod << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3273492864347793x):
#include<iostream>

#include<cstdio>

#include<queue>

#define LL long long



using namespace std;

const int mod = 1e9+7;



LL fun(LL k)

{

	return (k*(k+1)/2-k)%mod;

}



int main()

{

	LL n,k;

	cin>>n>>k;

	int a[2010];

	for(int i=0;i<n;i++) cin>>a[i];

	LL num1=0;

	for(int i=0;i<n;i++){

		for(int j=i;j<n;j++) 

		if(a[i]>a[j]) num1++;

	}

	if(k==1) {

		cout<<num1<<endl;

	}

	else {

		LL num2=0;

		for(int i=0;i<n;i++){

			for(int j=0;j<n;j++)

			if(a[i]>a[j]) num2++;

		}

		cout<<(num1*k%mod+fun(k)*num2%mod)%mod<<endl;

	}

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

typedef long long ll;

const int maxn=2e5+100;

const int mod=1e9+7;

int a[maxn];

int l[maxn];//记录左边比i要小的数 的个数 

int r[maxn];//记录右边比i要小的数 的个数

int main()

{

	int n,k;

	ll ans;

	ll sam;

	int a[maxn];

	int l[maxn];//记录左边比i要小的数 的个数 

	int r[maxn];//记录右边比i要小的数 的个数

	memset(l,0,sizeof(l));

	memset(r,0,sizeof(r));

	scanf("%d%d",&n,&k); 

	sam=(ll)(k+1)*k/2;//总和时要算右边比i小的次数 

	sam-=k; //总和时要算左边比i小的次数 

	sam=(sam+mod)%mod;

	for(int i=0;i<n;i++)

		scanf("%d",&a[i]);

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<i;j++)

			if(a[j]<a[i])

				l[i]++;

		for(int j=i+1;j<n;j++)

			if(a[j]<a[i])

				r[i]++;		 

	}

	ans=0; 

	for(int i=0;i<n;i++)

	{

		ans+=(l[i]*sam);

		ans%=mod;

		ans+=(r[i]*(sam+k));

		ans%=mod;

	}

	printf("%lld\n",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.828102109889209x):
#include<iostream>

#include<cstdio>

#include<queue>

#define LL long long



using namespace std;

const int mod = 1e9+7;



LL fun(LL k)

{

	return (k*(k+1)/2-k)%mod;

}



int main()

{

	LL n,k;

	cin>>n>>k;

	int a[2010];

	for(int i=0;i<n;i++) cin>>a[i];

	LL num1=0;

	for(int i=0;i<n;i++){

		for(int j=i;j<n;j++) 

		if(a[i]>a[j]) num1++;

	}

	if(k==1) {

		cout<<num1<<endl;

	}

	else {

		LL num2=0;

		for(int i=0;i<n;i++){

			for(int j=0;j<n;j++)

			if(a[i]>a[j]) num2++;

		}

		cout<<(num1*k%mod+fun(k)*num2%mod)%mod<<endl;

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 2e4 + 10;



ll a[MAXN], gall[MAXN], gback[MAXN];



int main() {

	ios::sync_with_stdio(false);

	ll n, k, mod = 1e9 + 7;

	while(cin >> n >> k) {

		memset(gall, 0, sizeof(gall));

		memset(gback, 0, sizeof(gback));

		for(int i = 0; i < n; i++) {

			cin >> a[i];

		}

		for(int i = 0; i < n; i++) {

			for(int j = 0; j < n; j++) {

				if(a[i] > a[j]) {

					gall[i]++;

					if(j > i) {

						gback[i]++;

					}

				}

			}

		}

		ll ans = 0;

		for(int i = 0; i < n; i++) {

			ans = (ans + k * gback[i] + (k * (k - 1) / 2 % mod) * gall[i] % mod) % mod;

		}

		cout << ans % mod << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.322317460625982x):
#include<iostream>

using namespace std;



typedef long long ll;

const ll mod=1e9+7;

ll f[2345678];

ll c[2345678];

ll d[2345678];





ll ksm(ll a,ll b){

	ll res = 1;

	while(b){

		if(b&1) res = res*a%mod;

		a=a*a%mod;

		b>>=1;

	}

	return res;

}

int main()

{

   int n;

   ll k,count=0;

   cin>>n>>k;

   for(int i=0;i<n;i++)  cin>>f[i];

   for(int i=0;i<n;i++)

   {

       for(int j=i+1;j<n;j++)

           if(f[i]>f[j]) c[i]++;

    

       for(int j=0;j<i;j++)

           if(f[i]>f[j]) d[i]++;

    }

    for(int i=0;i<n;i++)

    {    	

		count=(count+c[i]*k%mod*(1+k)%mod*ksm(2,mod-2)%mod)%mod;

    	count=(count+d[i]*(k-1)%mod*k%mod*ksm(2,mod-2)%mod)%mod;

	}

    cout<<count<<endl;

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

typedef long long ll;

const int maxn=2e5+100;

const int mod=1e9+7;

int a[maxn];

int l[maxn];//记录左边比i要小的数 的个数 

int r[maxn];//记录右边比i要小的数 的个数

int main()

{

	int n,k;

	ll ans;

	ll sam;

	int a[maxn];

	int l[maxn];//记录左边比i要小的数 的个数 

	int r[maxn];//记录右边比i要小的数 的个数

	memset(l,0,sizeof(l));

	memset(r,0,sizeof(r));

	scanf("%d%d",&n,&k); 

	sam=(ll)(k+1)*k/2;//总和时要算右边比i小的次数 

	sam-=k; //总和时要算左边比i小的次数 

	sam=(sam+mod)%mod;

	for(int i=0;i<n;i++)

		scanf("%d",&a[i]);

	for(int i=0;i<n;i++)

	{

		for(int j=0;j<i;j++)

			if(a[j]<a[i])

				l[i]++;

		for(int j=i+1;j<n;j++)

			if(a[j]<a[i])

				r[i]++;		 

	}

	ans=0; 

	for(int i=0;i<n;i++)

	{

		ans+=(l[i]*sam);

		ans%=mod;

		ans+=(r[i]*(sam+k));

		ans%=mod;

	}

	printf("%lld\n",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8211719887284368x):
#include<iostream>

using namespace std;



typedef long long ll;

const ll mod=1e9+7;

ll f[2345678];

ll c[2345678];

ll d[2345678];





ll ksm(ll a,ll b){

	ll res = 1;

	while(b){

		if(b&1) res = res*a%mod;

		a=a*a%mod;

		b>>=1;

	}

	return res;

}

int main()

{

   int n;

   ll k,count=0;

   cin>>n>>k;

   for(int i=0;i<n;i++)  cin>>f[i];

   for(int i=0;i<n;i++)

   {

       for(int j=i+1;j<n;j++)

           if(f[i]>f[j]) c[i]++;

    

       for(int j=0;j<i;j++)

           if(f[i]>f[j]) d[i]++;

    }

    for(int i=0;i<n;i++)

    {    	

		count=(count+c[i]*k%mod*(1+k)%mod*ksm(2,mod-2)%mod)%mod;

    	count=(count+d[i]*(k-1)%mod*k%mod*ksm(2,mod-2)%mod)%mod;

	}

    cout<<count<<endl;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 2e4 + 10;



ll a[MAXN], gall[MAXN], gback[MAXN];



int main() {

	ios::sync_with_stdio(false);

	ll n, k, mod = 1e9 + 7;

	while(cin >> n >> k) {

		memset(gall, 0, sizeof(gall));

		memset(gback, 0, sizeof(gback));

		for(int i = 0; i < n; i++) {

			cin >> a[i];

		}

		for(int i = 0; i < n; i++) {

			for(int j = 0; j < n; j++) {

				if(a[i] > a[j]) {

					gall[i]++;

					if(j > i) {

						gback[i]++;

					}

				}

			}

		}

		ll ans = 0;

		for(int i = 0; i < n; i++) {

			ans = (ans + k * gback[i] + (k * (k - 1) / 2 % mod) * gall[i] % mod) % mod;

		}

		cout << ans % mod << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.34650257073926x):
 #include <bits/stdc++.h>

    using namespace std;

    typedef long long ll;

    #define INPUT()              freopen("input.txt", "r", stdin)

    #define OUTPUT()             freopen("output.txt", "w", stdout)

    #define F                    first

    #define S                    second

//Templatefunctions

    template<typename T>T gcd(T x, T y) { if (y == 0)return x; else return gcd(y, x%y);}

    template <class T> T euclideanDistance(T x1 , T x2 , T y1 , T y2){return sqrt(( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ));}

    template <class T> T digitSum(T n){T sum = 0;while(n > 0)sum += n % 10,n /= 10;return sum;}

//datatypes

    typedef long long int ll;

    typedef unsigned long long int ull;

    typedef long double ld;

//constants

    const double INF = 1e+9;

    const double INFLL = 1e+18;

    const double EPS = 1e-9;

    const double PI = acos(-1);

    const int knightDir[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,-1},{2,1},{-1,-2},{1,-2}};

           //const int dx[]={0,0,0,1,1,1,-1,-1,-1};

           //const int dy[]={-1,0,1,-1,0,1,-1,0,1};

    const int dx[] = {0 , 1 , 0 , -1};

    const int dy[] = {1 , 0 , -1 , 0};

//helpers

    //#define MOD         1000000007LL

    #define MAX         2000005

    const ll MOD = 1000000000 + 7;

    //int arr[10000005];

    char str[MAX+10];

//Global Variables

    int dp[MAX],n;

    std::map<int,int>::iterator it;

    int ans;

    int main()

    {

//        int t;

//        scanf("%d",&t);

//        int x = 2;

//        while(t--)

//        {

//        }

        ll n , k;

        scanf("%lld%lld",&n,&k);



        for(int i = 0 ;i < n ; i++)

        {

            scanf("%d",&dp[i]);

        }

        vector<int> m,d;

        int cnt = 0, cnt2 = 0;

        for(int i = 0 ; i < n  ; i++)

        {

            for(int j = i+1 ; j < n ; j++)

            {

                if(dp[j] < dp[i])cnt++;

            }

            for(int j = 0 ; j < i ; j++)

            {

                if(dp[j] < dp[i])cnt2++;

            }

        }

        ll ans = 0;

        ll temp = (k * (k+1))/2L;

        temp -= k;

        temp = temp%MOD;

        //printf("temp-%lld\n",temp);

        ans += ((temp+k)*cnt)%MOD;

        ans += (temp*cnt2)%MOD;

        printf("%lld\n",ans%MOD);

    }

//