#include using namespace std; using Int = signed; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a struct Mint{ T v; Mint():v(0){} Mint(signed v):v(v){} Mint(long long t){v=t%MOD;if(v<0) v+=MOD;} Mint pow(long long k){ Mint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Mint add_identity(){return Mint(0);} static Mint mul_identity(){return Mint(1);} Mint inv(){return pow(MOD-2);} Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;} Mint& operator/=(Mint a){return (*this)*=a.inv();} Mint operator+(Mint a) const{return Mint(v)+=a;}; Mint operator-(Mint a) const{return Mint(v)-=a;}; Mint operator*(Mint a) const{return Mint(v)*=a;}; Mint operator/(Mint a) const{return Mint(v)/=a;}; Mint operator-() const{return v?Mint(MOD-v):Mint(v);} bool operator==(const Mint a)const{return v==a.v;} bool operator!=(const Mint a)const{return v!=a.v;} bool operator <(const Mint a)const{return v fact,finv,invs; static void init(Int n){ Int m=fact.size(); if(n=m;i--) finv[i-1]=finv[i]*Mint(i); for(Int i=m;i<=n;i++) invs[i]=finv[i]*fact[i-1]; } static Mint C(Int n,Int k){ if(n vector > Mint::fact = vector >(); template vector > Mint::finv = vector >(); template vector > Mint::invs = vector >(); using ll = long long; namespace NTT { constexpr Int bmds(Int x){ const Int v[] = {1012924417, 924844033, 998244353, 897581057, 645922817}; return v[x]; } constexpr Int brts(Int x){ const Int v[] = {5, 5, 3, 3, 3}; return v[x]; } template struct NTT{ static constexpr Int md = bmds(X); static constexpr Int rt = brts(X); inline Int add(Int a,Int b){ a+=b; if(a>=md) a-=md; return a; } inline Int mul(Int a,Int b){ return 1LL*a*b%md; } inline Int pow(Int a,Int b){ Int res=1; while(b){ if(b&1) res=mul(res,a); a=mul(a,a); b>>=1; } return res; } inline Int inv(Int x){ return pow(x,md-2); } vector > rts,rrts; void ensure_base(Int n){ if((Int)rts.size()>=n) return; rts.resize(n);rrts.resize(n); for(Int i=1;i &a,bool f,Int n=-1){ if(n==-1) n=a.size(); assert((n&(n-1))==0); for(Int i=0,j=1;j+1>1;k>(i^=k);k>>=1); if(i>j) swap(a[i],a[j]); } for(Int i=1;i multiply(vector &a,vector &b){ Int need=a.size()+b.size()-1; Int sz=1; while(sz f(sz),g(sz); for(Int i=0;i<(Int)a.size();i++) f[i]=a[i]; for(Int i=0;i<(Int)b.size();i++) g[i]=b[i]; ntt(f,0);ntt(g,0); for(Int i=0;i ntt0; NTT<1> ntt1; NTT<2> ntt2; inline Int powMod(Int n, Int p, Int m) { Int res = 1; while (p) { if (p & 1) res = (ll)res * n % m; n = (ll)n * n % m; p >>= 1; } return (Int)res; } inline Int invMod(Int n, Int m) { return powMod(n, m - 2, m); } constexpr Int M[] = {bmds(0), bmds(1), bmds(2)}; inline void garner(std::vector *c, Int mod) { size_t sz=c[0].size(); const Int R01 = invMod(M[0], M[1]); const Int R02 = invMod(M[0], M[2]); const Int R12 = invMod(M[1], M[2]); const Int M01 = (ll)M[0] * M[1] % mod; for (size_t i = 0; i < sz; ++i) { c[1][i] = (c[1][i] - c[0][i]) * (ll)R01 % M[1]; if (c[1][i] < 0) c[1][i] += M[1]; c[2][i] = ((c[2][i] - c[0][i]) * (ll)R02 % M[2] - c[1][i]) * R12 % M[2]; if (c[2][i] < 0) c[2][i] += M[2]; c[0][i] = (c[0][i] + (ll)c[1][i] * M[0] + (ll)c[2][i] * M01) % mod; } } std::vector mul(std::vector a, std::vector b, Int mod) { for (auto& x : a) x %= mod; for (auto& x : b) x %= mod; size_t m = a.size() + b.size() - 1; std::vector c[3]; c[0] = ntt0.multiply(a, b); c[1] = ntt1.multiply(a, b); c[2] = ntt2.multiply(a, b); for (auto& v : c) v.resize(m); garner(c, mod); return c[0]; } }; //INSERT ABOVE HERE signed main(){ Int n,b; cin>>n>>b; vector s(n); for(Int i=0;i>s[i]; using M = Mint; M::init(4e6); vector cnt(n,0); for(Int i=0;i > ; priority_queue

pq; pq.emplace(-1,vector(1,1)); Int sum=0; for(Int i=n-1;i>=0;i--){ if(cnt[i]==0) continue; M x=M::H(sum,cnt[i]); M y=M::H(sum+1,cnt[i])-x; x*=M::fact[cnt[i]]; y*=M::fact[cnt[i]]; pq.emplace(-2,vector({x.v,y.v})); sum+=cnt[i]; } const Int MOD = 1e9+7; while(pq.size()>1u){ auto a=pq.top().second;pq.pop(); auto b=pq.top().second;pq.pop(); auto c=NTT::mul(a,b,MOD); pq.emplace(-(Int)c.size(),c); } auto dp=pq.top().second; M ans(0),res(1); for(Int j=0;j<(Int)dp.size();j++){ ans+=M((ll)j*(ll)dp[j])*res; res*=M(b); } cout<