Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int mod = 998244353, maxn = 3e5 + 10;
int ksm(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = (ll)res * x % mod;
    y >>= 1;
    x = (ll)x * x % mod;
  }
  return res;
}
int fac[maxn], ifac[maxn];
int C(int n, int m) { return (ll)fac[n] * ifac[m] % mod * ifac[n - m] % mod; }
int main() {
  int n = read(), A = read(), B = read();
  int ans = 0;
  ll Caon = readll();
  fac[0] = 1;
  REP(i, 1, n) fac[i] = (ll)i * fac[i - 1] % mod;
  ifac[n] = ksm(fac[n], mod - 2);
  DREP(i, n - 1, 0) ifac[i] = (ll)(i + 1) * ifac[i + 1] % mod;
  REP(i, 0, n) {
    ll res = Caon - (ll)i * A;
    if (res % B)
      continue;
    ll j = res / B;
    if (j > n)
      continue;
    ans = ((ll)C(n, i) * C(n, j) + ans) % mod;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c > '9' || c < '0'))
    ;
  if (c == '-')
    f = -1, c = getchar();
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int mod = 998244353, maxn = 3e5 + 10;
int ksm(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = (ll)res * x % mod;
    y >>= 1;
    x = (ll)x * x % mod;
  }
  return res;
}
int fac[maxn], ifac[maxn];
int C(int n, int m) { return (ll)fac[n] * ifac[m] % mod * ifac[n - m] % mod; }
int main() {
  int n = read(), A = read(), B = read();
  int ans = 0;
  ll Caon = readll();
  fac[0] = 1;
  REP(i, 1, n) fac[i] = (ll)i * fac[i - 1] % mod;
  ifac[n] = ksm(fac[n], mod - 2);
  DREP(i, n - 1, 0) ifac[i] = (ll)(i + 1) * ifac[i + 1] % mod;
  REP(i, 0, n) {
    ll res = Caon - (ll)i * A;
    if (res % B || res < 0)
      continue;
    ll j = res / B;
    if (j > n)
      continue;
    ans = ((ll)C(n, i) * C(n, j) + ans) % mod;
  }
  printf("%d\n", ans);
  return 0;
}