#include #pragma GCC optimize("O3") #pragma GCC target("avx") #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #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; 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; } return ret; } template class NTT { public: inline int get_mod() const { return mod; } inline void _ntt(vector& a, int sign){ const int n = len(a); const int g = 3; //g is primitive root of mod int h = mod_pow(g, (mod - 1) / n, mod); // h^n = 1 if(sign == -1) h = mod_pow(h, mod-2, mod); //h = h^-1 % mod int s,x,m,j,k; int i = 0; for(j = 1; j < n - 1; ++j){ for(k = n >> 1; k >(i ^= k); k >>= 1); if (j < i) swap(a[i], a[j]); } for (m = 1; m < n; m *= 2){ const int m2 = 2 * m; const int base = mod_pow(h, n / m2, mod); int w = 1; for(x = 0; x < m; x++){ for(s = x; s < n; s += m2){ unsigned int u = a[s]; unsigned int d = (ll)a[s + m] * w % mod; a[s] = (u + d) % mod; a[s + m] = (u + mod - d) % mod; } w = (ll)w * base % mod; } } } inline void ntt(vector& input){ _ntt(input, 1); } inline void intt(vector& input){ _ntt(input, -1); const int n_inv = mod_pow(len(input), mod-2, mod); for(auto& x : input) x = (ll)x * n_inv % mod; } inline void convolution(const vector& a, const vector& b, vector& _a, vector& _b){ int ntt_size = 1; while (ntt_size < len(a) + len(b)) ntt_size *= 2; _a = a, _b = b; _a.resize(ntt_size); _b.resize(ntt_size); ntt(_a), ntt(_b); rep(i, ntt_size) _a[i] = (ll)_a[i] * _b[i] % mod; intt(_a); } }; typedef NTT<167772161, 3> NTT_1; typedef NTT<469762049, 3> NTT_2; typedef NTT<1224736769, 3> NTT_3; inline void fast_int32mod_convolution(const vector& a, const vector& b, vector& res){ NTT_1 ntt1; NTT_2 ntt2; NTT_3 ntt3; vector x,_x,y,_y,z,_z; ntt1.convolution(a, b, x, _x), ntt2.convolution(a, b, y, _y), ntt3.convolution(a, b, z, _z); const int m1 = ntt1.get_mod(), m2 = ntt2.get_mod(), m3 = ntt3.get_mod(); const int m1_inv_m2 = mod_pow(m1, m2-2, m2); const int m12_inv_m3 = mod_pow((ll)m1 * m2 % m3, m3-2, m3); const int m12_mod = (ll)m1 * m2 % MOD; res.resize(len(x)); rep(i, len(x)){ int v1 = ((ll)y[i] + m2 - x[i]) * m1_inv_m2 % m2; int v2 = ((ll)z[i] + m3 - (x[i] + (ll)m1 * v1) % m3) * m12_inv_m3 % m3; res[i] = (x[i] + (ll)m1 * v1 + (ll)m12_mod * v2) % MOD; } } vector 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] = {prod(id+cnt[i]-1, cnt[i]) % MOD, (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(); fast_int32mod_convolution(c[p], c[q], c[num]); 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; }