Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long

using Int = long long;
// BEGIN CUT HERE
#define MOD 998244353
#define MAX_P 200005
Int fact[MAX_P], inv[MAX_P], finv[MAX_P];
;
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
Int mod_inverse(Int a, Int mod) {
  Int x, y;
  extgcd(a, mod, x, y);
  return (mod + x % mod) % mod;
}

Int mod_pow(Int x, Int n, Int mod) {
  Int res = 1;
  while (n) {
    if (n & 1)
      (res *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return res;
}

Int mod_inverse2(Int a, Int mod) { return mod_pow(a, mod - 2, mod); }

void init(Int mod) {
  fact[0] = 1;
  for (Int i = 1; i < MAX_P; i++)
    fact[i] = (fact[i - 1] * i) % mod;

  inv[1] = 1;
  for (Int i = 2; i < MAX_P; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;

  finv[0] = 1;
  for (Int i = 1; i < MAX_P; i++)
    finv[i] = finv[i - 1] * inv[i] % mod;
}

Int mod_fact(Int n, Int mod, Int &e) {
  e = 0;
  if (n == 0)
    return 1;
  Int res = mod_fact(n / mod, mod, e);
  e += n / mod;
  if (n / mod % 2 != 0)
    return res * (mod - fact[n % mod]) % mod;
  return res * fact[n % mod] % mod;
}

Int mod_comb(Int n, Int k, Int mod) {
  if (n == k || k == 0)
    return 1;
  Int e1, e2, e3;
  Int a1 = mod_fact(n, mod, e1), a2 = mod_fact(k, mod, e2),
      a3 = mod_fact(n - k, mod, e3);
  if (e1 > e2 + e3)
    return 0;
  return a1 * mod_inverse(a2 * a3 % mod, mod) % mod;
}

Int mod_comb2(Int n, Int k, Int mod) {
  Int res = 1;
  for (Int i = 0; i < k; i++) {
    res *= (n - i) % mod;
    res %= mod;
    res *= mod_inverse(i + 1, mod);
    res %= mod;
  }
  return res;
}

// only for prime mod
Int mod_comb3(Int n, Int k, Int mod) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * finv[k] % mod * finv[n - k] % mod;
}

signed main() {
  init(MOD);

  int n, a, b, k;
  cin >> n >> a >> b >> k;

  int ans = 0;

  for (int i = 0; i <= n; i++) {
    if (a * i <= k && (k - a * i) % b == 0) {
      int j = (k - a * i) / b;
      if (i > n || j > n)
        continue;
      ans += mod_comb3(n, i, MOD) * mod_comb3(n, j, MOD);
      ans %= MOD;
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long

using Int = long long;
// BEGIN CUT HERE
#define MOD 998244353
#define MAX_P 300005
Int fact[MAX_P], inv[MAX_P], finv[MAX_P];
;
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
Int mod_inverse(Int a, Int mod) {
  Int x, y;
  extgcd(a, mod, x, y);
  return (mod + x % mod) % mod;
}

Int mod_pow(Int x, Int n, Int mod) {
  Int res = 1;
  while (n) {
    if (n & 1)
      (res *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return res;
}

Int mod_inverse2(Int a, Int mod) { return mod_pow(a, mod - 2, mod); }

void init(Int mod) {
  fact[0] = 1;
  for (Int i = 1; i < MAX_P; i++)
    fact[i] = (fact[i - 1] * i) % mod;

  inv[1] = 1;
  for (Int i = 2; i < MAX_P; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;

  finv[0] = 1;
  for (Int i = 1; i < MAX_P; i++)
    finv[i] = finv[i - 1] * inv[i] % mod;
}

Int mod_fact(Int n, Int mod, Int &e) {
  e = 0;
  if (n == 0)
    return 1;
  Int res = mod_fact(n / mod, mod, e);
  e += n / mod;
  if (n / mod % 2 != 0)
    return res * (mod - fact[n % mod]) % mod;
  return res * fact[n % mod] % mod;
}

Int mod_comb(Int n, Int k, Int mod) {
  if (n == k || k == 0)
    return 1;
  Int e1, e2, e3;
  Int a1 = mod_fact(n, mod, e1), a2 = mod_fact(k, mod, e2),
      a3 = mod_fact(n - k, mod, e3);
  if (e1 > e2 + e3)
    return 0;
  return a1 * mod_inverse(a2 * a3 % mod, mod) % mod;
}

Int mod_comb2(Int n, Int k, Int mod) {
  Int res = 1;
  for (Int i = 0; i < k; i++) {
    res *= (n - i) % mod;
    res %= mod;
    res *= mod_inverse(i + 1, mod);
    res %= mod;
  }
  return res;
}

// only for prime mod
Int mod_comb3(Int n, Int k, Int mod) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * finv[k] % mod * finv[n - k] % mod;
}

signed main() {
  init(MOD);

  int n, a, b, k;
  cin >> n >> a >> b >> k;

  int ans = 0;

  for (int i = 0; i <= n; i++) {
    if (a * i <= k && (k - a * i) % b == 0) {
      int j = (k - a * i) / b;
      if (i > n || j > n)
        continue;
      ans += mod_comb3(n, i, MOD) * mod_comb3(n, j, MOD);
      ans %= MOD;
    }
  }

  cout << ans << endl;
  return 0;
}