Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define rep(i, m, n) for (int i = (m); i < (n); i++)
#define P pair<ll, ll>
#define pb push_back
#define mk make_pair
using namespace std;
#define Vec(a) vector<int> a
const int INF = 1 << 20;

const ll MOD = 998244353;

const int MAX_N = 400010;
ll fac[MAX_N], inv[MAX_N];
ll qpow(ll x, int k) {
  x = x % MOD;
  ll ans = 1;
  while (k) {
    if (k & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    k >>= 1;
  }
  return ans;
}
void init() {
  fac[0] = 1;
  for (int i = 1; i < MAX_N; i++)
    fac[i] = fac[i - 1] * i % MOD;
  inv[MAX_N - 1] = qpow(fac[MAX_N - 1], MOD - 2);
  for (int i = MAX_N - 1; i > 0; i--)
    inv[i - 1] = inv[i] * i % MOD;
}
ll Comb(int n, int m) { return fac[n] * inv[m] % MOD * inv[n - m] % MOD; }

/*
int N,K;
ll mo=998244353;

ll combi(ll N_, ll C_) {
        const int NUM_=1400001;
        static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
        if (fact[0]==0) {
                inv[1]=fact[0]=factr[0]=1;
                for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) %
mo; for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo,
factr[i]=factr[i-1]*inv[i]%mo;
        }
        if(C_<0 || C_>N_) return 0;
        return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}
*/
int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  vector<P> c;
  init();
  REP(i, n + 1) {
    if (((k - b * i) % a) == 0 && (k - b * i) >= 0 && (k - b * i) / a <= n) {
      c.pb(mk((k - b * i) / a, i));
      // cout << (k-b*i)/a << "  " << i << endl;
    }
  }
  ll sum = 0;
  REP(i, c.size()) {
    REP(j, min(c[i].first, c[i].second) + 1) {
      sum =
          (sum + ((Comb(n, c[i].first - j) % 998244353 *
                   Comb(n - (c[i].first - j), j) % 998244353) %
                  998244353 * Comb(n - (c[i].first - j) - j, c[i].second - j) %
                  998244353)) %
          998244353;
      // cout << ((Comb(n,c[i].first-j)*Comb(n-(c[i].first-j),
      // j)%998244353)*Comb(n-(c[i].first-j)-j, c[i].second-j)%998244353) <<
      // endl;
    }
  }
  sum %= 998244353;
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define rep(i, m, n) for (int i = (m); i < (n); i++)
#define P pair<ll, ll>
#define pb push_back
#define mk make_pair
using namespace std;
#define Vec(a) vector<int> a
const int INF = 1 << 20;

const ll MOD = 998244353;

const int MAX_N = 400010;
ll fac[MAX_N], inv[MAX_N];
ll qpow(ll x, int k) {
  x = x % MOD;
  ll ans = 1;
  while (k) {
    if (k & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    k >>= 1;
  }
  return ans;
}
void init() {
  fac[0] = 1;
  for (int i = 1; i < MAX_N; i++)
    fac[i] = fac[i - 1] * i % MOD;
  inv[MAX_N - 1] = qpow(fac[MAX_N - 1], MOD - 2);
  for (int i = MAX_N - 1; i > 0; i--)
    inv[i - 1] = inv[i] * i % MOD;
}
ll Comb(int n, int m) { return fac[n] * inv[m] % MOD * inv[n - m] % MOD; }

/*
int N,K;
ll mo=998244353;

ll combi(ll N_, ll C_) {
        const int NUM_=1400001;
        static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
        if (fact[0]==0) {
                inv[1]=fact[0]=factr[0]=1;
                for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) %
mo; for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo,
factr[i]=factr[i-1]*inv[i]%mo;
        }
        if(C_<0 || C_>N_) return 0;
        return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}
*/
int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  vector<P> c;
  init();
  REP(i, n + 1) {
    if (((k - b * i) % a) == 0 && (k - b * i) >= 0 && (k - b * i) / a <= n) {
      c.pb(mk((k - b * i) / a, i));
      // cout << (k-b*i)/a << "  " << i << endl;
    }
  }
  ll sum = 0;
  REP(i, c.size()) {

    sum =
        (sum +
         ((Comb(n, c[i].first) % 998244353 * Comb(n, c[i].second) % 998244353) %
          998244353)) %
        998244353;
  }
  sum %= 998244353;
  cout << sum << endl;
  return 0;
}