#include #include #include #include using ll = long long; using ull = unsigned long long; constexpr ll MOD = 1000000007; constexpr int MAX = 200000; int R[MAX]; ll fact[2 * MAX + 1]; ll inv[2 * MAX + 1]; constexpr std::size_t PC(ull v) { return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast(v * 0x0101010101010101ULL >> 56 & 0x7f); } constexpr std::size_t LG(ull v) { return v == 0 ? 0 : (v--, v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), PC(v)); } constexpr ull SZ(const ull v) { return 1ULL << LG(v); } template constexpr std::pair extgcd(const T a, const T b) { if (b == 0) { return std::pair{1, 0}; } const auto p = extgcd(b, a % b); return {p.second, p.first - p.second * (a / b)}; } template constexpr T inverse(const T a, const T mod = MOD) { return (mod + extgcd(a, mod).first % mod) % mod; } template class NumberTheoreticTransformation { public: static std::vector convolute(const std::vector& a, const std::vector& b) // ans[i] = \sum_{A+B = i} a[A]*b[B] { const std::size_t size = a.size() + b.size() - 1, t = (std::size_t)SZ(size); std::vector A(t, 0), B(t, 0); for (std::size_t i = 0; i < a.size(); i++) { A[i] = a[i]; } for (std::size_t i = 0; i < b.size(); i++) { B[i] = b[i]; } ntt(A), ntt(B); for (std::size_t i = 0; i < t; i++) { A[i] = mul(A[i], B[i]); } ntt(A, true); A.resize(size); return A; } private: NumberTheoreticTransformation() = delete; static T add(const T x, const T y) { return (x + y < mod) ? x + y : x + y - mod; } static T mul(const T x, const T y) { return (x * y) % mod; } static T power(const T x, const T n) { return n == 0 ? (T)1 : n % 2 == 1 ? mul(power(x, n - 1), x) : power(mul(x, x), n / 2); } static T inverse(const T x) { return power(x, mod - 2); } static void ntt(std::vector& a, const bool rev = false) { const std::size_t size = a.size(), height = LG(size); for (std::size_t i = 0, j = 0; i < size; i++, j = 0) { for (std::size_t k = 0; k < height; k++) { j |= (i >> k & 1) << (height - 1 - k); } if (i < j) { std::swap(a[i], a[j]); } } for (std::size_t i = 1; i < size; i <<= 1) { T w = power(root, (mod - 1) / (i * 2)); if (not rev) { w = inverse(w); } for (std::size_t j = 0; j < size; j += i * 2) { T wn = 1; for (std::size_t k = 0; k < i; k++, wn = mul(wn, w)) { const T s = a[j + k + 0], t = mul(a[j + k + i], wn); a[j + k + 0] = add(s, t), a[j + k + i] = add(s, mod - t); } } } if (not rev) { return; } const T v = inverse(size); for (std::size_t i = 0; i < size; i++) { a[i] = mul(a[i], v); } } }; template class GarnerNumberTheoreticTransformation { public: static std::vector convolute(const std::vector& a, const std::vector& b, const T mod) // ans[i] = \sum_{A+B = i} a[A]*b[B] { const auto x = NTT1::convolute(a, b), y = NTT2::convolute(a, b), z = NTT3::convolute(a, b); const std::size_t size = x.size(); std::vector ans(size); const T mod1mod2_mod = mod1 * mod2 % mod; for (std::size_t i = 0; i < size; i++) { T v1 = (y[i] - x[i]) * mod1_inv_mod2 % mod2; if (v1 < 0) { v1 += mod2; } T v2 = (z[i] - (x[i] + mod1 * v1) % mod3) * mod1mod2_inv_mod3 % mod3; if (v2 < 0) { v2 += mod3; } T c = (x[i] + mod1 * v1 + mod1mod2_mod * v2) % mod; if (c < 0) { c += mod; } ans[i] = c; } return ans; } private: static constexpr T mod1 = 167772161; static constexpr T mod2 = 469762049; static constexpr T mod3 = 1224736769; static constexpr T mod1_inv_mod2 = inverse(mod1, mod2); static constexpr T mod1mod2_inv_mod3 = inverse(mod1 * mod2, mod3); using NTT1 = NumberTheoreticTransformation; using NTT2 = NumberTheoreticTransformation; using NTT3 = NumberTheoreticTransformation; GarnerNumberTheoreticTransformation() = delete; }; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N; ll P; std::cin >> N >> P; std::fill(fact, fact + 2 * MAX + 1, 1), std::fill(inv, inv + 2 * MAX + 1, 1); for (ll i = 2; i <= 2 * N; i++) { fact[i] = (fact[i - 1] * i) % MOD, inv[i] = ((MOD - MOD / i) * inv[MOD % i]) % MOD; } for (int i = 1; i <= 2 * N; i++) { (inv[i] *= inv[i - 1]) %= MOD; } for (int i = 0, a; i < N; i++) { std::cin >> a, R[a]++; } std::deque> fs{{{1}}}; for (int i = N - 1, k = 0; i >= 0; k += R[i], i--) { const int r = R[i]; if (r == 0) { continue; } const ll alpha = (r * fact[r + k - 1] % MOD) * inv[k] % MOD; const ll beta = (k * fact[r + k - 1] % MOD) * inv[k] % MOD; fs.push_back({beta, alpha}); } while (fs.size() > 1) { const auto f1 = fs.front(); fs.pop_front(); const auto f2 = fs.front(); fs.pop_front(); const auto f3 = GarnerNumberTheoreticTransformation::convolute(f1, f2, MOD); fs.push_back(f3); } const auto f = fs.front(); ll ans = 0; for (ll i = 0, p = 1; i < f.size(); i++, (p *= P) %= MOD) { (ans += (p * i % MOD) * f[i] % MOD) %= MOD; } std::cout << ans << std::endl; }