Problem p03332 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma vector temporal
// #pragma simd
// #pragma GCC diagnostic ignored "-W"

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define ll long long
#define ld long double
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vint vector<int>
#define SS stringstream
#define PQ priority_queue
#define MS(x, v) memset((x), (v), sizeof(x))
#define RZUNI(x)                                                               \
  sort(x.begin(), x.end()), x.resize(unique(x.begin(), x.end()) - x.begin())
#define FLH fflush(stdout)
#define CPPinput                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define FIN(fname) freopen(fname, "r", stdin)
#define FOUT(fname) freopen(fname, "w", stdout)

#define tm Ovuvuevuevue
#define y1 Enyetuenwuevue
#define left Ugbemugbem
#define ws Osas
#define dec tetteterette
#define exp expexpexpexp
#define expl explexplexpl

#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl

#ifdef WEAK
#include "/home/edison/Coding/cpp/template/debug.cpp"
#define DEB(...) printf(__VA_ARGS__), fflush(stdout)
#define WHR()                                                                  \
  printf("%s: Line %d", __PRETTY_FUNCTION__, __LINE__), fflush(stdout)
#define LOG(...)                                                               \
  printf("%s: Line %d ", __PRETTY_FUNCTION__, __LINE__), printf(__VA_ARGS__),  \
      fflush(stdout)
#define DEBUG 1
#define exit(x) cout << "exit code " << x << endl, exit(0)
#else
#define PDE(...) ;
#define DEB(...) ;
#define WHR() ;
#define LOG(...) ;
#define DEBUG 0
#endif

#define lowbit(x) ((x) & (-(x)))

#if __cplusplus >= 201103L
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#endif

void JIZZ(string output = "") {
  cout << output;
  exit(0);
}

const ld PI = 3.14159265358979323846264338327950288;
const ld eps = 1e-13;
const ll mod = 998244353;

long long fac[300005], ifac[300005];

long long pw(long long b, long long n, long long m, long long a = 1) {
  while (n) {
    if (n & 1)
      a = a * b % m;
    b = b * b % m;
    n >>= 1;
  }
  return a;
}
long long C(int n, int i) { return ifac[i] * ifac[n - i] % mod * fac[n] % mod; }

int main() {
  CPPinput;
  long long n, a, b, k;
  cin >> n >> a >> b >> k;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = fac[i - 1] * i % mod;
  ifac[n] = pw(fac[n], mod - 2, mod);
  for (int i = n - 1; i >= 0; --i)
    ifac[i] = ifac[i + 1] * (i + 1) % mod;
  long long ans = 0;
  for (int i = 0; i <= n; ++i) {
    long long ua = i * a;
    if (ua > k || (k - ua) % b)
      continue;
    long long ub = k - ua;
    int j = ub / b;
    ans += C(n, i) * C(n, j) % mod;
  }
  cout << ans % mod << endl;
}


Fixed submission:
// #pragma GCC optimize("no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma vector temporal
// #pragma simd
// #pragma GCC diagnostic ignored "-W"

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define ll long long
#define ld long double
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vint vector<int>
#define SS stringstream
#define PQ priority_queue
#define MS(x, v) memset((x), (v), sizeof(x))
#define RZUNI(x)                                                               \
  sort(x.begin(), x.end()), x.resize(unique(x.begin(), x.end()) - x.begin())
#define FLH fflush(stdout)
#define CPPinput                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define FIN(fname) freopen(fname, "r", stdin)
#define FOUT(fname) freopen(fname, "w", stdout)

#define tm Ovuvuevuevue
#define y1 Enyetuenwuevue
#define left Ugbemugbem
#define ws Osas
#define dec tetteterette
#define exp expexpexpexp
#define expl explexplexpl

#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl

#ifdef WEAK
#include "/home/edison/Coding/cpp/template/debug.cpp"
#define DEB(...) printf(__VA_ARGS__), fflush(stdout)
#define WHR()                                                                  \
  printf("%s: Line %d", __PRETTY_FUNCTION__, __LINE__), fflush(stdout)
#define LOG(...)                                                               \
  printf("%s: Line %d ", __PRETTY_FUNCTION__, __LINE__), printf(__VA_ARGS__),  \
      fflush(stdout)
#define DEBUG 1
#define exit(x) cout << "exit code " << x << endl, exit(0)
#else
#define PDE(...) ;
#define DEB(...) ;
#define WHR() ;
#define LOG(...) ;
#define DEBUG 0
#endif

#define lowbit(x) ((x) & (-(x)))

#if __cplusplus >= 201103L
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#endif

void JIZZ(string output = "") {
  cout << output;
  exit(0);
}

const ld PI = 3.14159265358979323846264338327950288;
const ld eps = 1e-13;
const ll mod = 998244353;

long long fac[300005], ifac[300005];

long long pw(long long b, long long n, long long m, long long a = 1) {
  while (n) {
    if (n & 1)
      a = a * b % m;
    b = b * b % m;
    n >>= 1;
  }
  return a;
}
long long C(int n, int i) { return ifac[i] * ifac[n - i] % mod * fac[n] % mod; }

int main() {
  CPPinput;
  long long n, a, b, k;
  cin >> n >> a >> b >> k;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = fac[i - 1] * i % mod;
  ifac[n] = pw(fac[n], mod - 2, mod);
  for (int i = n - 1; i >= 0; --i)
    ifac[i] = ifac[i + 1] * (i + 1) % mod;
  long long ans = 0;
  for (int i = 0; i <= n; ++i) {
    long long ua = i * a;
    if (ua > k || (k - ua) % b)
      continue;
    long long ub = k - ua;
    int j = ub / b;
    if (j > n)
      continue;
    ans += C(n, i) * C(n, j) % mod;
  }
  cout << ans % mod << endl;
}