Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
//--------------------------------------------------------------------------------//
ll modpow(ll a, ll n, ll mod_) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod_;
    a = a * a % mod_;
    n >>= 1;
  }
  return res;
}

const ll mod = 998244353;
// const ll mod = 1e9 + 7;
const ll MAX_SIZE = 1000005;
array<ll, MAX_SIZE> fac, inv, finv;
ll fac_init() {
  // combination init
  fac[0] = 1;
  for (ll i = 1; i < MAX_SIZE; i++)
    fac[i] = fac[i - 1] * i % mod;
  finv[0] = modpow(fac[MAX_SIZE - 1], mod - 2, mod);
  repe(i, 1, MAX_SIZE) finv[i] = finv[i - 1] * (MAX_SIZE - i) % mod;
  reverse(all(finv));

  // inv init
  repe(i, 1, MAX_SIZE) inv[i] = modpow(i, mod - 2, mod);
}

ll perm(ll a, ll b) { return fac[a] * finv[a - b] % mod; }

ll comb(ll a, ll b) { return fac[a] * finv[b] % mod * finv[a - b] % mod; }

// ax+by=gcd(a,b)を満たすx,yを求める
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll gc = a;
  if (b != 0) {
    gc = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1, y = 0;
  }
  return gc;
}
// 値A, B
// ll x, y;
// extgcd(A, B, x, y); //x,yにAx+By=gcd(a,b)を満たす値を返す

int main() {
  init();
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  if (N == 0) {
    cout << 1 << endl;
    return 0;
  }

  fac_init();
  ll ans = 0;
  rep(a, N + 1) {
    ll dk = K - a * A;
    ll b = dk / B;
    if (dk % B || b > N)
      continue;

    ans += comb(N, a) * comb(N, b) % mod;
  }
  cout << ans % mod << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
//--------------------------------------------------------------------------------//
ll modpow(ll a, ll n, ll mod_) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod_;
    a = a * a % mod_;
    n >>= 1;
  }
  return res;
}

const ll mod = 998244353;
// const ll mod = 1e9 + 7;
const ll MAX_SIZE = 1000005;
array<ll, MAX_SIZE> fac, inv, finv;
ll fac_init() {
  // combination init
  fac[0] = 1;
  for (ll i = 1; i < MAX_SIZE; i++)
    fac[i] = fac[i - 1] * i % mod;
  finv[0] = modpow(fac[MAX_SIZE - 1], mod - 2, mod);
  repe(i, 1, MAX_SIZE) finv[i] = finv[i - 1] * (MAX_SIZE - i) % mod;
  reverse(all(finv));

  // inv init
  repe(i, 1, MAX_SIZE) inv[i] = modpow(i, mod - 2, mod);
}

ll perm(ll a, ll b) { return fac[a] * finv[a - b] % mod; }

ll comb(ll a, ll b) { return fac[a] * finv[b] % mod * finv[a - b] % mod; }

// ax+by=gcd(a,b)を満たすx,yを求める
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll gc = a;
  if (b != 0) {
    gc = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1, y = 0;
  }
  return gc;
}
// 値A, B
// ll x, y;
// extgcd(A, B, x, y); //x,yにAx+By=gcd(a,b)を満たす値を返す

int main() {
  init();
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  if (N == 0) {
    cout << 1 << endl;
    return 0;
  }

  fac_init();
  ll ans = 0;
  rep(a, N + 1) {
    ll dk = K - a * A;
    if (dk < 0)
      break;
    ll b = dk / B;
    if (dk % B || b > N)
      continue;

    ans += comb(N, a) * comb(N, b) % mod;
  }
  cout << ans % mod << endl;
}