#include #pragma GCC optimize("O3") #pragma GCC target("avx") #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-9 #define rep(i,n) for(int i=0;i<(int)(n);++i) #define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i) #define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i) #define each(a,b) for(auto& (a): (b)) #define all(v) (v).begin(),(v).end() #define len(v) (int)(v).size() #define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end()) #define cmx(x,y) x=max(x,y) #define cmn(x,y) x=min(x,y) #define fi first #define se second #define pb push_back #define show(x) cout<<#x<<" = "<<(x)< P; typedef pair pll; typedef vector vi; typedef vector vvi; typedef vector vl; typedef vector vvl; typedef vector vd; typedef vector

vp; typedef vector vs; typedef double dbl; typedef pair pi; const int MAX_N = 200000; #define getchar getchar_unlocked inline int in() { int n = 0; short c; while ((c = getchar()) >= '0') n = n * 10 + c - '0'; return n; } int inv[MAX_N],fac[MAX_N],finv[MAX_N]; void make() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for(int i=2;i> 1] >> 1) + ((i & 1) << (base - 1)); } void fft(num *a, num *f) { rep(i, N) f[i] = a[rev[i]]; for (int k = 1; k < N; k <<= 1) for (int i = 0; i < N; i += 2 * k) rep(j, k) { num z = f[i + j + k] * root[j + k]; f[i + j + k] = f[i + j] - z; f[i + j] = f[i + j] + z; } } num a[maxN], b[maxN], f[maxN], g[maxN]; ll A[maxN], B[maxN], C[maxN]; void _multMod(int mod) { rep(i, N) { int x = A[i] % mod; a[i] = num(x & (pw(15) - 1), x >> 15); } rep(i, N) { int x = B[i] % mod; b[i] = num(x & (pw(15) - 1), x >> 15); } fft(a, f); fft(b, g); rep(i, N) { int j = (N - i) & (N - 1); num a1 = (f[i] + conj(f[j])) * num(0.5, 0); num a2 = (f[i] - conj(f[j])) * num(0, -0.5); num b1 = (g[i] + conj(g[j])) * num(0.5 / N, 0); num b2 = (g[i] - conj(g[j])) * num(0, -0.5 / N); a[j] = a1 * b1 + a2 * b2 * num(0, 1); b[j] = a1 * b2 + a2 * b1; } fft(a, f); fft(b, g); rep(i, N) { ll aa = f[i].x + 0.5; ll bb = g[i].x + 0.5; ll cc = f[i].y + 0.5; C[i] = (aa + bb % mod * pw(15) + cc % mod * pw(30)) % mod; } } void prepAB(int n1, int n2) { base = 1; N = 2; while (N < n1 + n2) base++, N <<= 1; assert(base <= maxBase); for (int i = n1; i < N; ++i) A[i] = 0; for (int i = n2; i < N; ++i) B[i] = 0; prepRoots(); prepRev(); } void multMod(int n1, int n2, int mod) { prepAB(n1, n2); _multMod(mod); } } struct poly { vi v; poly() {} poly(vi vv) { v = vv; } int size() { return (int)v.size(); } poly cut(int maxLen) { if (maxLen < len(v)) v.resize(maxLen); return *this; } poly norm() { while (len(v) > 1 && v.back() == 0) v.pop_back(); return *this; } inline int& operator [] (int i) { return v[i]; } }; poly operator + (poly A, poly B) { poly C; C.v = vi(max(len(A), len(B))); rep(i, len(C)) { if (i < len(A)) C[i] = (C[i] + A[i]) % MOD; if (i < len(B)) C[i] = (C[i] + B[i]) % MOD; } return C.norm(); } poly operator - (poly A, poly B) { poly C; C.v = vi(max(len(A), len(B))); rep(i, len(C)) { if (i < len(A)) C[i] = (C[i] + A[i]) % MOD; if (i < len(B)) C[i] = (C[i] + MOD - B[i]) % MOD; } return C.norm(); } poly operator * (poly A, poly B) { poly C; C.v = vi(len(A) + len(B) - 1); rep(i, len(A)) fft::A[i] = A[i]; rep(i, len(B)) fft::B[i] = B[i]; fft::multMod(len(A), len(B), MOD); rep(i, len(C)) C[i] = fft::C[i]; return C.norm(); } poly c[2*MAX_N]; int cnt[MAX_N]; int main() { int n = in(), B = in(); rep(i,n){ cnt[in()]++; } make(); int id = 0, num = 0; priority_queue, greater

> que; rrep(i,MAX_N){ if(!cnt[i]) continue; c[num] = (vi){prod(id+cnt[i]-1, cnt[i]) % MOD, (int)((ll)prod(id+cnt[i]-1, cnt[i]-1) * cnt[i] % MOD)}; que.push(P(2, num++)); id += cnt[i]; } while(len(que) >= 2){ int p = que.top().se; que.pop(); int q = que.top().se; que.pop(); c[num] = c[p] * c[q]; que.push(P(len(c[num]), num)); num++; } int index = que.top().se; int ans = 0, nB = 1; rep(i, len(c[index])){ ans = (ans + (((ll)c[index][i] * i) % MOD) * nB) % MOD; nB = (ll)nB * B % MOD; } cout << ans << "\n"; return 0; }