Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define mp make_pair
#define pb push_back
using namespace std;

typedef long long li;
template <class T> inline void read(T &x) {
  x = 0;
  T tmp = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    tmp = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + c - '0', c = getchar();
  x *= tmp;
}
template <class T> inline void Max(T &x, T y) {
  if (y > x)
    x = y;
}
template <class T> inline void Min(T &x, T y) {
  if (y < x)
    x = y;
}

const int Mod = 998244353;
inline int Add(int x, int y) {
  x += y;
  if (x >= Mod)
    x -= Mod;
  if (x < 0)
    x += Mod;
  return x;
}
inline int Mul(int x, int y) { return 1LL * x * y % Mod; }
inline int Pow(int a, int b) {
  int res = (a == 0) ? 0 : 1;
  while (b) {
    if (b & 1)
      res = Mul(res, a);
    a = Mul(a, a);
    b >>= 1;
  }
  return res;
}
const int N = 1e5 + 10;
int n;
li A, B, K;
int inv[N], fac[N];

inline int C(int n, int m) {
  if (n <= m || m == 0)
    return 1;
  return Mul(fac[n], Mul(inv[m], inv[n - m]));
}

int main(void) {
  read(n), read(A), read(B), read(K);

  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = Mul(fac[i - 1], i);
  inv[n] = Pow(fac[n], Mod - 2);
  for (int i = n; i; --i)
    inv[i - 1] = Mul(inv[i], i);

  int ans = 0;
  li limx = min(K / A, 1LL * n);
  for (int x = 0; x <= limx; ++x) {
    li tmp = K - 1LL * x * A;
    if (tmp % B != 0)
      continue;
    int y = tmp / B;
    if (y > n)
      continue;
    ans = Add(ans, Mul(C(n, x), C(n, y)));
  }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define mp make_pair
#define pb push_back
using namespace std;

typedef long long li;
template <class T> inline void read(T &x) {
  x = 0;
  T tmp = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-')
    c = getchar();
  if (c == '-')
    tmp = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + c - '0', c = getchar();
  x *= tmp;
}
template <class T> inline void Max(T &x, T y) {
  if (y > x)
    x = y;
}
template <class T> inline void Min(T &x, T y) {
  if (y < x)
    x = y;
}

const int Mod = 998244353;
inline int Add(int x, int y) {
  x += y;
  if (x >= Mod)
    x -= Mod;
  if (x < 0)
    x += Mod;
  return x;
}
inline int Mul(int x, int y) { return 1LL * x * y % Mod; }
inline int Pow(int a, int b) {
  int res = (a == 0) ? 0 : 1;
  while (b) {
    if (b & 1)
      res = Mul(res, a);
    a = Mul(a, a);
    b >>= 1;
  }
  return res;
}
const int N = 3e5 + 10;
int n;
li A, B, K;
int inv[N], fac[N];

inline int C(int n, int m) {
  if (n <= m || m == 0)
    return 1;
  return Mul(fac[n], Mul(inv[m], inv[n - m]));
}

int main(void) {
  read(n), read(A), read(B), read(K);

  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = Mul(fac[i - 1], i);
  inv[n] = Pow(fac[n], Mod - 2);
  for (int i = n; i; --i)
    inv[i - 1] = Mul(inv[i], i);

  int ans = 0;
  li limx = min(K / A, 1LL * n);
  for (int x = 0; x <= limx; ++x) {
    li tmp = K - 1LL * x * A;
    if (tmp % B != 0)
      continue;
    int y = tmp / B;
    if (y > n)
      continue;
    ans = Add(ans, Mul(C(n, x), C(n, y)));
  }
  printf("%d\n", ans);

  return 0;
}