Language: C++

//#include "SaveFuncs.cpp"

/*****/

#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <iomanip>	//cout << fixed << setprecision(桁数);
#include <stdexcept>

using namespace std;
using ll = long long;

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep1(i, n) for(int i=1, i##_len=(n); i<i##_len; i++)
#define rrep(i, n) for(int i=(n)-1; i >= 0; i--)
#define rrep1(i, n) for(int i=(n); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define pair(a, b) make_pair(a, b);
//#define int long long
constexpr int INF = 2000000100;
constexpr ll DIV = 1000000007;	// 10e9+7
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> T divUp(T a, T b) { if (a % b == 0) { return a / b; } return a / b + 1; }
std::string strMulti(std::string t, int n) { std::string out = ""; for (int i = 0; i < n; i++) { out += t; } return out; }
template<class T> T math_P(T m, T n) { T ret = 1; for (T i = m; i > m - n; i--) { ret *= i; } return ret; }
template<class T> T math_C(T m, T n) { T ret = mathP(m, n); for (T i = 2; i <= n; i++) { ret /= i; } return ret; }
template<class T> bool compare_by_b(pair<T, T> a, pair<T, T> b) { if (a.second != b.second) { return a.second < b.second; } return a.first < b.first; }
template<class T> T math_gcd(T a, T b) { a = abs(a); b = abs(b); if (a < b) { std::swap(a, b); } T tmp; while (b != 0) { tmp = b; b = a % b; a = tmp; } return a; }
template<class T> T math_lcm(T a, T b) { a = abs(a); b = abs(b); if (a > b) { std::swap(a, b); } if (a == 1) { return b; } T i = a; while (i % b != 0) { i += a; } return i; }

/*****/

int Main()
{
	int N;
	ll K;
	cin >> N >> K;
	vector<int> A(N);
	rep(i, N)	cin >> A.at(i);

	vector<int> Head(N), Down(N);
	rep(i, N)
	{
		for (int j = 0; j < i; j++)
		{
			if (A.at(j) < A.at(i))
			{
				Head.at(i)++;
				Down.at(i)++;
				break;
			}
		}
		for (int j = i; j < N; j++)
		{
			if (A.at(j) < A.at(i))
			{
				Down.at(i)++;
				break;
			}
		}
	}

	rep(i, N)
	{
		debug_headCout(i, Down.at(i));
	}
	rep(i, N)
	{
		debug_headCout(i, Head.at(i));
	}

	ll ret = 0;
	ll max = K * (K + 1) / 2;
	debug_cout(max);
	max %= DIV;
	debug_cout(max);
	rep(i, N)
	{
		ret -= Head.at(i);
		debug_cout(i);
		ret += Down.at(i) * max;
		if (ret > DIV)
		{
			ret %= DIV;
		}
	}

	ret %= DIV;

	cout << ret;

	return 0;
}

/*****/

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

	//std::cout << std::fixed << std::setprecision(10);

	Main();

	return 0;
}


Compiler output:
a.cc: In function 'int Main()':
a.cc:73:17: error: 'debug_headCout' was not declared in this scope
   73 |                 debug_headCout(i, Down.at(i));
      |                 ^~~~~~~~~~~~~~
a.cc:77:17: error: 'debug_headCout' was not declared in this scope
   77 |                 debug_headCout(i, Head.at(i));
      |                 ^~~~~~~~~~~~~~
a.cc:82:9: error: 'debug_cout' was not declared in this scope
   82 |         debug_cout(max);
      |         ^~~~~~~~~~