Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int, int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = b - 1; i >= a; i--)
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define Yes(i) cout << (i ? "Yes" : "No") << endl
using namespace std;
//
const int INF = 1e9 + 7;
const int mod = 998244353;
//
struct poi {
  int X;
  int Y;
  int Z;
  bool operator<(const poi &R) const {
    return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X;
  }
};
/*
ll GCD(ll A,ll B){
  return (B==0)?(A):(GCD(B,A%B));
}
ll LCM(ll A,ll B){
  return A/GCD(A,B)*B;
}
*/
ll N, A, B, K;
ll kai[300001];
ll gya[300001];
ll nck[300001];
//
ll pow(ll x, ll n, ll mod) {
  ll res = 1;
  if (n > 0) {
    res = pow(x, n / 2, mod);
    if (n % 2 == 0) {
      res = (res * res) % mod;
    } else {
      res = (((res * res) % mod) * x) % mod;
    }
  }
  return res;
}
//
ll inv(ll n, ll mod) { return pow(n, mod - 2, mod); }
//
void nCk(ll n, ll mod) {
  kai[n] = n;
  ROF(i, 1, n) { (kai[i] = kai[i + 1] * i) %= mod; }
  gya[1] = inv(1, mod);
  FOR(i, 2, n + 1) { (gya[i] = gya[i - 1] * inv(i, mod)) %= mod; }
  FOR(i, 1, n) { (nck[i] = kai[N + 1 - i] * gya[i]) %= mod; }
  nck[0] = 1;
  nck[n] = 1;
}
int main() {
  cin >> N >> A >> B >> K;
  nCk(N, mod);
  ll ans = 0;
  FOR(i, 0, N + 1) {
    ll a = i;
    ll b = (K - a * A) / B;
    if (K == A * a + B * b) {
      (ans += nck[a] * nck[b]) %= mod;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int, int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = b - 1; i >= a; i--)
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define Yes(i) cout << (i ? "Yes" : "No") << endl
using namespace std;
//
const int INF = 1e9 + 7;
const int mod = 998244353;
//
struct poi {
  int X;
  int Y;
  int Z;
  bool operator<(const poi &R) const {
    return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X;
  }
};
/*
ll GCD(ll A,ll B){
  return (B==0)?(A):(GCD(B,A%B));
}
ll LCM(ll A,ll B){
  return A/GCD(A,B)*B;
}
*/
ll N, A, B, K;
ll kai[300001];
ll gya[300001];
ll nck[300001];
//
ll pow(ll x, ll n, ll mod) {
  ll res = 1;
  if (n > 0) {
    res = pow(x, n / 2, mod);
    if (n % 2 == 0) {
      res = (res * res) % mod;
    } else {
      res = (((res * res) % mod) * x) % mod;
    }
  }
  return res;
}
//
ll inv(ll n, ll mod) { return pow(n, mod - 2, mod); }
//
void nCk(ll n, ll mod) {
  kai[n] = n;
  ROF(i, 1, n) { (kai[i] = kai[i + 1] * i) %= mod; }
  gya[1] = inv(1, mod);
  FOR(i, 2, n + 1) { (gya[i] = gya[i - 1] * inv(i, mod)) %= mod; }
  FOR(i, 1, n) { (nck[i] = kai[N + 1 - i] * gya[i]) %= mod; }
  nck[0] = 1;
  nck[n] = 1;
}
int main() {
  cin >> N >> A >> B >> K;
  nCk(N, mod);
  ll ans = 0;
  FOR(i, 0, N + 1) {
    ll a = i;
    ll b = (K - a * A) / B;
    if (K == A * a + B * b && 0 <= a && a <= N && 0 <= b && b <= N) {
      (ans += nck[a] * nck[b]) %= mod;
    }
  }
  cout << ans << endl;
  return 0;
}