#include #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) begin(x), end(x) #define dump(x) cerr << #x " = " << x << endl using ll = long long; using namespace std; template using reversed_priority_queue = priority_queue, greater >; template inline void chmax(T & a, U const & b) { a = max(a, b); } template inline void chmin(T & a, U const & b) { a = min(a, b); } template auto make_table(X x, T a) { return vector(x, a); } template auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector(x, cont); } template ostream & operator << (ostream & out, vector const & xs) { REP (i, (int)xs.size() - 1) out << xs[i] << ' '; if (not xs.empty()) out << xs.back(); return out; } template struct fix_type { Functor functor; template decltype(auto) operator() (Args && ... args) const & { return functor(functor, std::forward(args)...); } }; template fix_type::type> fix(Functor && functor) { return { std::forward(functor) }; } inline constexpr int32_t modpow(uint_fast64_t x, uint64_t k, int32_t MOD) { assert (0 <= x and x < MOD); uint_fast64_t y = 1; for (; k; k >>= 1) { if (k & 1) (y *= x) %= MOD; (x *= x) %= MOD; } assert (0 <= y and y < MOD); return y; } inline int32_t modinv(int32_t value, int32_t MOD) { assert (0 <= value and value < MOD); assert (value != 0); int64_t a = value, b = MOD; int64_t x = 0, y = 1; for (int64_t u = 1, v = 0; a; ) { int64_t q = b / a; x -= q * u; std::swap(x, u); y -= q * v; std::swap(y, v); b -= q * a; std::swap(b, a); } assert (value * x + MOD * y == b and b == 1); if (x < 0) x += MOD; assert (0 <= x and x < MOD); return x; } template struct mint { int32_t value; mint() : value() {} mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {} mint(int32_t value_, nullptr_t) : value(value_) {} explicit operator bool() const { return value; } inline constexpr mint operator + (mint other) const { return mint(*this) += other; } inline constexpr mint operator - (mint other) const { return mint(*this) -= other; } inline constexpr mint operator * (mint other) const { return mint(*this) *= other; } inline constexpr mint & operator += (mint other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; } inline constexpr mint & operator -= (mint other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; } inline constexpr mint & operator *= (mint other) { this->value = (uint_fast64_t)this->value * other.value % MOD; return *this; } inline constexpr mint operator - () const { return mint(this->value ? MOD - this->value : 0, nullptr); } inline constexpr mint pow(uint64_t k) const { return mint(modpow(value, k, MOD), nullptr); } inline mint inv() const { return mint(modinv(value, MOD), nullptr); } inline constexpr mint operator / (mint other) const { return *this * other.inv(); } inline constexpr mint operator /= (mint other) { return *this *= other.inv(); } inline constexpr bool operator == (mint other) const { return value == other.value; } inline constexpr bool operator != (mint other) const { return value != other.value; } }; template mint operator * (int64_t value, mint n) { return mint(value) * n; } template std::istream & operator >> (std::istream & in, mint & n) { int64_t value; in >> value; n = value; return in; } template std::ostream & operator << (std::ostream & out, mint n) { return out << n.value; } template struct proth_prime {}; template <> struct proth_prime<1224736769> { static constexpr int a = 73, b = 24, g = 3; }; template <> struct proth_prime<1053818881> { static constexpr int a = 3 * 5 * 67, b = 20, g = 7; }; template <> struct proth_prime<1051721729> { static constexpr int a = 17 * 59, b = 20, g = 6; }; template <> struct proth_prime<1045430273> { static constexpr int a = 997, b = 20, g = 3; }; template <> struct proth_prime<1012924417> { static constexpr int a = 3 * 7 * 23, b = 21, g = 5; }; template <> struct proth_prime<1007681537> { static constexpr int a = 31 * 31, b = 20, g = 3; }; template <> struct proth_prime<1004535809> { static constexpr int a = 479, b = 21, g = 3; }; template <> struct proth_prime< 998244353> { static constexpr int a = 7 * 17, b = 23, g = 3; }; template <> struct proth_prime< 985661441> { static constexpr int a = 5 * 47, b = 22, g = 3; }; template <> struct proth_prime< 976224257> { static constexpr int a = 7 * 7 * 19, b = 20, g = 3; }; template <> struct proth_prime< 975175681> { static constexpr int a = 3 * 5 * 31, b = 21, g = 17; }; template <> struct proth_prime< 962592769> { static constexpr int a = 3 * 3 * 3 * 17, b = 21, g = 7; }; template <> struct proth_prime< 950009857> { static constexpr int a = 4 * 151, b = 21, g = 7; }; template <> struct proth_prime< 943718401> { static constexpr int a = 3 * 3 * 5 * 5, b = 22, g = 7; }; template <> struct proth_prime< 935329793> { static constexpr int a = 223, b = 22, g = 3; }; template <> struct proth_prime< 924844033> { static constexpr int a = 3 * 3 * 7 * 7, b = 21, g = 5; }; template <> struct proth_prime< 469762049> { static constexpr int a = 7, b = 26, g = 3; }; template <> struct proth_prime< 167772161> { static constexpr int a = 5, b = 25, g = 3; }; struct is_proth_prime_impl { template static auto check(T *) -> decltype(proth_prime::g, std::true_type()); template static auto check(...) -> std::false_type; }; template struct is_proth_prime : decltype(is_proth_prime_impl::check(nullptr)) { }; /** * @note O(N log N) * @note radix-2, decimation-in-frequency, Cooley-Tukey * @note cache std::polar (~ 2x faster) */ template void ntt_inplace(std::vector > & a, bool inverse) { const int n = a.size(); const int log2_n = __builtin_ctz(n); assert (n == 1 << log2_n); assert (log2_n <= proth_prime::b); // prepare rotors std::vector > ep, iep; while ((int)ep.size() <= log2_n) { ep.push_back(mint(proth_prime::g).pow(mint(-1).value / (1 << ep.size()))); iep.push_back(ep.back().inv()); } // divide and conquer std::vector > b(n); REP3 (i, 1, log2_n + 1) { int w = 1 << (log2_n - i); mint base = (inverse ? iep : ep)[i]; mint now = 1; for (int y = 0; y < n / 2; y += w) { REP (x, w) { auto l = a[y << 1 | x]; auto r = now * a[y << 1 | x | w]; b[y | x] = l + r; b[y | x | n >> 1] = l - r; } now *= base; } std::swap(a, b); } // div by n if inverse if (inverse) { auto n_inv = mint(n).inv(); REP (i, n) { a[i] *= n_inv; } } } /* * @brief a specialized version of Garner's algorithm */ template mint garner_algorithm(mint a1, mint a2, mint a3) { static const auto r12 = mint(MOD1).inv(); static const auto r13 = mint(MOD1).inv(); static const auto r23 = mint(MOD2).inv(); a2 = (a2 - a1.value) * r12; a3 = (a3 - a1.value) * r13; a3 = (a3 - a2.value) * r23; return mint(a1.value) + a2.value * mint(MOD1) + a3.value * (mint(MOD1) * mint(MOD2)); } /* * @arg eqns is equations like x = a_i (mod m_i) * @return the minimal solution of given equations */ int32_t garner_algorithm(std::vector > eqns, int32_t MOD) { eqns.emplace_back(0, MOD); std::vector k(eqns.size(), 1); std::vector c(eqns.size(), 0); REP (i, eqns.size() - 1) { int32_t a_i, m_i; std::tie(a_i, m_i) = eqns[i]; int32_t x = (a_i - c[i]) * modinv(k[i], m_i) % m_i; if (x < 0) x += m_i; assert (a_i == (k[i] * x + c[i]) % m_i); REP3 (j, i + 1, eqns.size()) { int32_t a_j, m_j; std::tie(a_j, m_j) = eqns[j]; (c[j] += k[j] * x) %= m_j; (k[j] *= m_i) %= m_j; } } return c.back(); } template std::vector > ntt_convolution_small(const std::vector > & a, const std::vector > & b) { std::vector > c(a.size() + b.size() - 1); REP (i, a.size()) { REP (j, b.size()) { c[i + j] += a[i] * b[j]; } } return c; } /** * @brief the convolution on Z/pZ * @note O(N log N) * @note (f \ast g)(i) = \sum_{0 \le j \lt i + 1} f(j) g(i - j) */ template typename std::enable_if::value, std::vector > >::type ntt_convolution(const std::vector > & a_, const std::vector > & b_) { if (a_.size() <= 8 or b_.size() <= 8) return ntt_convolution_small(a_, b_); int m = a_.size() + b_.size() - 1; int n = (m == 1 ? 1 : 1 << (32 - __builtin_clz(m - 1))); auto a = a_; auto b = b_; a.resize(n); b.resize(n); ntt_inplace(a, false); ntt_inplace(b, false); REP (i, n) { a[i] *= b[i]; } ntt_inplace(a, true); return a; } template typename std::enable_if::value, std::vector > >::type ntt_convolution(const std::vector > & a, const std::vector > & b) { if (a.size() <= 8 or b.size() <= 8) return ntt_convolution_small(a, b); constexpr int PRIMES[3] = { 1004535809, 998244353, 985661441 }; std::vector > x0(a.size()); std::vector > x1(a.size()); std::vector > x2(a.size()); REP (i, a.size()) { x0[i] = a[i].value; x1[i] = a[i].value; x2[i] = a[i].value; } std::vector > y0(b.size()); std::vector > y1(b.size()); std::vector > y2(b.size()); REP (j, b.size()) { y0[j] = b[j].value; y1[j] = b[j].value; y2[j] = b[j].value; } std::vector > z0 = ntt_convolution(x0, y0); std::vector > z1 = ntt_convolution(x1, y1); std::vector > z2 = ntt_convolution(x2, y2); std::vector > c(z0.size()); REP (k, z0.size()) { c[k] = garner_algorithm(z0[k], z1[k], z2[k]); } return c; } template struct formal_power_series { std::vector a; inline size_t size() const { return a.size(); } inline bool empty() const { return a.empty(); } inline T at(int i) const { return (i < size() ? a[i] : T(0)); } inline const std::vector & data() const { return a; } formal_power_series() = default; formal_power_series(const std::vector & a_) : a(a_) { shrink(); } formal_power_series(const std::initializer_list & init) : a(init) { shrink(); } template formal_power_series(Iterator first, Iterator last) : a(first, last) { shrink(); } void shrink() { while (not a.empty() and a.back().value == 0) a.pop_back(); } inline formal_power_series operator + (const formal_power_series & other) const { return formal_power_series(a) += other; } inline formal_power_series operator - (const formal_power_series & other) const { return formal_power_series(a) -= other; } inline formal_power_series & operator += (const formal_power_series & other) { if (a.size() < other.a.size()) a.resize(other.a.size(), T(0)); REP (i, other.a.size()) a[i] += other.a[i]; return *this; } inline formal_power_series & operator -= (const formal_power_series & other) { if (a.size() < other.a.size()) a.resize(other.a.size(), T(0)); REP (i, other.a.size()) a[i] -= other.a[i]; return *this; } inline formal_power_series operator * (const formal_power_series & other) const { return formal_power_series(ntt_convolution(a, other.a)); } inline formal_power_series operator * (T b) { return formal_power_series(a) *= b; } inline formal_power_series & operator *= (T b) { REP (i, size()) a[i] *= b; return *this; } inline formal_power_series integral() const { std::vector b(size() + 1); REP (i, size()) { b[i + 1] = a[i] / (i + 1); } return b; } inline formal_power_series differential() const { if (empty()) return *this; std::vector b(size() - 1); REP (i, size() - 1) { b[i] = a[i + 1] * (i + 1); } return b; } inline formal_power_series modulo_x_to(int k) const { return formal_power_series(a.begin(), a.begin() + std::min(size(), k)); } formal_power_series inv(int n) const { assert (at(0) != 0); formal_power_series res { at(0).inv() }; for (int i = 1; i < n; i *= 2) { res = (res * T(2) - res * res * modulo_x_to(2 * i)).modulo_x_to(2 * i); } return res.modulo_x_to(n); } formal_power_series exp(int n) const { formal_power_series f{ 1 }; formal_power_series g{ 1 }; for (int i = 1; i < n; i *= 2) { g = (g * 2 - f * g * g).modulo_x_to(i); formal_power_series q = differential().modulo_x_to(i - 1); formal_power_series w = (q + g * (f.differential() - f * q)).modulo_x_to(2 * i - 1); f = (f + f * (*this - w.integral()).modulo_x_to(2 * i)).modulo_x_to(2 * i); } return f.modulo_x_to(n); } inline formal_power_series log(int n) const { assert (at(0) == 1); return (this->differential() * this->inv(n - 1)).modulo_x_to(n - 1).integral(); } inline formal_power_series pow(int k, int n) const { return (this->log(n) * k).exp(n); } }; constexpr int MOD = 1000000007; mint solve(int64_t n, int64_t m, int64_t d1, int64_t d2) { int64_t m1 = m - 1 - (n - 1) * d1; int64_t d3 = d2 - d1; if (m1 < 0) return 0; if (d3 == 0) return m1 + 1; formal_power_series > f(vector >(min(m1, d3) + 1, 1)); formal_power_series > g(vector >(m1 + 1, 1)); vector > dp = (f.pow(n - 1, m1 + 1) * g).modulo_x_to(m1 + 1).data(); return accumulate(ALL(dp), mint()); } int main() { int64_t n, m, d1, d2; cin >> n >> m >> d1 >> d2; cout << solve(n, m, d1, d2) << endl; assert (is_proth_prime<998244353>::value); assert (not is_proth_prime<3>::value); return 0; }