Problem p03332 (C++)

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

#define LL long long
#define LD long double
#define SC(t, x) static_cast<t>(x)
#define AR(t) vector<t>
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define PIL pair<int, LL>
#define PLI pair<LL, int>
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define PRF first
#define PRS second
#define INIT(ar, val) memset(ar, val, sizeof(ar))
#define lp(loop, start, end) for (int loop = start; loop < end; ++loop)
#define lpd(loop, start, end) for (int loop = start; loop > end; --loop)
#define lpi(loop, start, end) for (int loop = start; loop <= end; ++loop)
#define lpdi(loop, start, end) for (int loop = start; loop >= end; --loop)
#define qmax(a, b) (((a) > (b)) ? (a) : (b))
#define qmin(a, b) (((a) < (b)) ? (a) : (b))
#define qabs(a) (((a) >= 0) ? (a) : (-(a)))

const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 300007;
const int MOD = 998244353;

int n, a, b;
LL K;
LL frac[MAXN], rf[MAXN];

void init();
void input();
void work();

int add(int x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
  return x;
}

void addv(int &x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += MOD;
  return x;
}

void subv(int &x, int y) {
  x -= y;
  if (x < 0)
    x += MOD;
}

int qpow(int a, int b) {
  LL base = a, ans = 1;
  while (b) {
    if (b & 1)
      (ans *= base) %= MOD;
    (base *= base) %= MOD;
    b >>= 1;
  }
  return SC(int, ans);
}

int C(int n, int m) { return SC(int, frac[n] * rf[m] % MOD * rf[n - m] % MOD); }

int main() {
  init();
  input();
  work();
}

void init() {
  // Init Everything Here

  ios::sync_with_stdio(false);

  frac[0] = 1;
  lp(i, 1, MAXN) frac[i] = frac[i - 1] * i % MOD;
  rf[MAXN - 1] = qpow(frac[MAXN - 1], MOD - 2);
  lpdi(i, MAXN - 2, 0) rf[i] = rf[i + 1] * (i + 1) % MOD;
}

void input() {
  // input method

  cin >> n >> a >> b >> K;
}

void work() {
  // main work

  int ans = 0;
  lpi(i, 0, n) {
    LL rem = K - SC(LL, a) * i;
    if (rem % b == 0 && rem / b <= n) {
      int j = rem / b;
      addv(ans, SC(LL, C(n, i)) * C(n, j) % MOD);
    }
  }

  cout << ans << endl;
}

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

#define LL long long
#define LD long double
#define SC(t, x) static_cast<t>(x)
#define AR(t) vector<t>
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define PIL pair<int, LL>
#define PLI pair<LL, int>
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define PRF first
#define PRS second
#define INIT(ar, val) memset(ar, val, sizeof(ar))
#define lp(loop, start, end) for (int loop = start; loop < end; ++loop)
#define lpd(loop, start, end) for (int loop = start; loop > end; --loop)
#define lpi(loop, start, end) for (int loop = start; loop <= end; ++loop)
#define lpdi(loop, start, end) for (int loop = start; loop >= end; --loop)
#define qmax(a, b) (((a) > (b)) ? (a) : (b))
#define qmin(a, b) (((a) < (b)) ? (a) : (b))
#define qabs(a) (((a) >= 0) ? (a) : (-(a)))

const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 300007;
const int MOD = 998244353;

int n, a, b;
LL K;
LL frac[MAXN], rf[MAXN];

void init();
void input();
void work();

int add(int x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
  return x;
}

void addv(int &x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += MOD;
  return x;
}

void subv(int &x, int y) {
  x -= y;
  if (x < 0)
    x += MOD;
}

int qpow(int a, int b) {
  LL base = a, ans = 1;
  while (b) {
    if (b & 1)
      (ans *= base) %= MOD;
    (base *= base) %= MOD;
    b >>= 1;
  }
  return SC(int, ans);
}

int C(int n, int m) { return SC(int, frac[n] * rf[m] % MOD * rf[n - m] % MOD); }

int main() {
  init();
  input();
  work();
}

void init() {
  // Init Everything Here

  ios::sync_with_stdio(false);

  frac[0] = 1;
  lp(i, 1, MAXN) frac[i] = frac[i - 1] * i % MOD;
  rf[MAXN - 1] = qpow(frac[MAXN - 1], MOD - 2);
  lpdi(i, MAXN - 2, 0) rf[i] = rf[i + 1] * (i + 1) % MOD;
}

void input() {
  // input method

  cin >> n >> a >> b >> K;
}

void work() {
  // main work

  int ans = 0;
  lpi(i, 0, n) {
    LL rem = K - SC(LL, a) * i;
    if (rem >= 0 && rem % b == 0 && rem / b <= n) {
      int j = rem / b;
      addv(ans, SC(LL, C(n, i)) * C(n, j) % MOD);
    }
  }

  cout << ans << endl;
}