Problem p03332 (C++)

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

using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < n; (i)++)
#define rep1(i, n) for (int(i) = 1; (i) <= n; (i)++)
#define repU(i, bottom, ceiling)                                               \
  for (int(i) = (bottom); (i) <= (ceiling); (i)++)
#define repD(i, ceiling, bottom)                                               \
  for (int(i) = (ceiling); (i) >= (bottom); (i)--)
#define pub push_back
#define pob pop_back
#define prf printf
#define scf scanf
#define vci(v)                                                                 \
  for (int i = 0; i < v.size(); i++)                                           \
  cin >> v[i]
#define vco(v)                                                                 \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i] << ' ';                                                       \
  }                                                                            \
  cout << endl
#define mp make_pair
#define fir first
#define sec second
#define clr(arr) memset((arr), 0, sizeof(arr))

typedef long long int ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<double> vdb;
typedef vector<vector<double>> vvdb;
typedef vector<long long int> vll;
typedef vector<vector<long long int>> vvll;
typedef pair<int, int> pii;

const string alphabet("abcdefghijklmnopqrstuvwxyz");
const vector<pair<int, int>> dir = {{1, 0}, {0, 1},  {-1, 0},  {0, -1},
                                    {1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
const long double Pi = 3.141592653589793238462643383279502884197;
const long double Napier = 2.71828182845904523536;
const int Inf = (int)2e9 + 1e8;
const int Mod = (int)998244353;

template <typename T, typename U>
inline pair<T, U> operator+(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir + r.fir, l.sec + r.sec};
}

template <typename T, typename U>
inline pair<T, U> operator-(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir - r.fir, l.sec - r.sec};
}

template <typename T, typename U>
inline pair<T, U> operator*(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir * r.fir, l.sec * r.sec};
}

template <typename T, typename U>
inline pair<T, U> operator/(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir / r.fir, l.sec / r.sec};
}

template <typename T> inline void Swap(T *x, T *y) {
  T tem = *x;
  *x = *y;
  *y = tem;
}

/*
int thre1,thre2;
vi v;

template<typename T> inline bool judge(T n){
        return (thre1<=v[n]&&thre2>=v[n]);
}

template<typename T> inline T BiS(T bottom,T ceiling,bool thretype){
        T upper=ceiling+thretype;
        T lower=bottom-(!thretype);
        while(upper-lower>1){
                T mid=(upper+lower)/2;
                if(judge(mid)) (thretype?lower:upper)=mid;
                else (thretype?upper:lower)=mid;
        }
        return (thretype?lower:upper);
}
*/

/* The main code follows below */

inline int Power(int n, int e, int mod) {
  long long res = 1;
  long long pow = n;
  while (e) {
    if (e & 1)
      res = (res * pow) % mod;
    pow = (pow * pow) % mod;
    e >>= 1;
  }
  return res;
}

int Rev[300003], Stair[300003];

inline int Comb(int n, int r, int mod) {
  if (n < r || min(n, r) < 0)
    return 0;
  return ((ll)Stair[n] * Rev[r]) % mod;
}

int N, A, B;
ll K;

inline void solve() {
  int Ans = 0;
  int memo[300003];
  Stair[0] = 1;
  rep(i, N) Stair[i + 1] = ((ll)Stair[i] * (i + 1)) % Mod;
  rep(i, N + 1) Rev[i] = Power((ll)Stair[i] * Stair[N - i] % Mod, Mod - 2, Mod);
  rep(i, N + 1) memo[i] = Comb(N, i, Mod);
  rep(i, N + 1) {
    ll red = K - A * i;
    if (red >= 0 && !(red % B)) {
      Ans = ((ll)memo[i] * memo[red / B] + Ans) % Mod;
    }
  }
  cout << Ans << endl;
  return;
}

inline void input() {
  cin >> N >> A >> B >> K;
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  input();
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < n; (i)++)
#define rep1(i, n) for (int(i) = 1; (i) <= n; (i)++)
#define repU(i, bottom, ceiling)                                               \
  for (int(i) = (bottom); (i) <= (ceiling); (i)++)
#define repD(i, ceiling, bottom)                                               \
  for (int(i) = (ceiling); (i) >= (bottom); (i)--)
#define pub push_back
#define pob pop_back
#define prf printf
#define scf scanf
#define vci(v)                                                                 \
  for (int i = 0; i < v.size(); i++)                                           \
  cin >> v[i]
#define vco(v)                                                                 \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i] << ' ';                                                       \
  }                                                                            \
  cout << endl
#define mp make_pair
#define fir first
#define sec second
#define clr(arr) memset((arr), 0, sizeof(arr))

typedef long long int ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<double> vdb;
typedef vector<vector<double>> vvdb;
typedef vector<long long int> vll;
typedef vector<vector<long long int>> vvll;
typedef pair<int, int> pii;

const string alphabet("abcdefghijklmnopqrstuvwxyz");
const vector<pair<int, int>> dir = {{1, 0}, {0, 1},  {-1, 0},  {0, -1},
                                    {1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
const long double Pi = 3.141592653589793238462643383279502884197;
const long double Napier = 2.71828182845904523536;
const int Inf = (int)2e9 + 1e8;
const int Mod = (int)998244353;

template <typename T, typename U>
inline pair<T, U> operator+(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir + r.fir, l.sec + r.sec};
}

template <typename T, typename U>
inline pair<T, U> operator-(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir - r.fir, l.sec - r.sec};
}

template <typename T, typename U>
inline pair<T, U> operator*(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir * r.fir, l.sec * r.sec};
}

template <typename T, typename U>
inline pair<T, U> operator/(const pair<T, U> &l, const pair<T, U> &r) {
  return {l.fir / r.fir, l.sec / r.sec};
}

template <typename T> inline void Swap(T *x, T *y) {
  T tem = *x;
  *x = *y;
  *y = tem;
}

/*
int thre1,thre2;
vi v;

template<typename T> inline bool judge(T n){
        return (thre1<=v[n]&&thre2>=v[n]);
}

template<typename T> inline T BiS(T bottom,T ceiling,bool thretype){
        T upper=ceiling+thretype;
        T lower=bottom-(!thretype);
        while(upper-lower>1){
                T mid=(upper+lower)/2;
                if(judge(mid)) (thretype?lower:upper)=mid;
                else (thretype?upper:lower)=mid;
        }
        return (thretype?lower:upper);
}
*/

/* The main code follows below */

inline int Power(int n, int e, int mod) {
  long long res = 1;
  long long pow = n;
  while (e) {
    if (e & 1)
      res = (res * pow) % mod;
    pow = (pow * pow) % mod;
    e >>= 1;
  }
  return res;
}

int Rev[300003], Stair[300003];

inline int Comb(int n, int r, int mod) {
  if (n < r || min(n, r) < 0)
    return 0;
  return ((ll)Stair[n] * Rev[r]) % mod;
}

int N, A, B;
ll K;

inline void solve() {
  int Ans = 0;
  int memo[300003];
  Stair[0] = 1;
  rep(i, N) Stair[i + 1] = ((ll)Stair[i] * (i + 1)) % Mod;
  rep(i, N + 1) Rev[i] = Power((ll)Stair[i] * Stair[N - i] % Mod, Mod - 2, Mod);
  rep(i, N + 1) memo[i] = Comb(N, i, Mod);
  rep(i, N + 1) {
    ll red = K - A * i;
    if (red >= 0 && !(red % B) && red / B <= N) {
      Ans = ((ll)memo[i] * memo[red / B] + Ans) % Mod;
    }
  }
  cout << Ans << endl;
  return;
}

inline void input() {
  cin >> N >> A >> B >> K;
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  input();
  solve();
  return 0;
}