#include #include #include #include using namespace std; const int N = 200010, MOD = 1000000007; int n, b, ans, a[N]; bool st[N]; int QMI(int a, int k) { int ret = 1 % MOD; while (k) { if (k & 1) { ret = 1LL * ret * a % MOD; } a = 1LL * a * a % MOD; k >>= 1; } return ret; } void DFS(int rem, int ma, int cnt, int pre) { if (rem == 0) { // cout << "rem: " << rem << " ma: " << ma << " cnt: " << cnt << " pre: " << pre << " " << 1LL * cnt * QMI(b, cnt) % MOD << endl; ans = (0LL + ans + 1LL * cnt * QMI(b, cnt) % MOD) % MOD; return; } for (int i = 1; i <= n; ++i) { if (!st[i]) { st[i] = true; if (a[i] > pre) ++cnt; DFS(rem - 1, max(ma, a[i]), cnt, max(pre, a[i])); if (a[i] > pre) --cnt; st[i] = false; } } } int main() { // freopen("growth.in", "r", stdin); // freopen("growth.out", "w", stdout); scanf("%d%d", &n, &b); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } DFS(n, 0, 0, -1); printf("%d\n", ans); return 0; }