Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/rope>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
//
// template<class T> using Tree = tree<T, null_type, less<T>,
// rb_t_tag,t_order_statistics_node_update>;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compiles and gets accepted.
 *  ___  __     _______    _______
 * |\  \|\  \  |\  ___ \  |\  ___ \
 * \ \  \/  /|_\ \   __/| \ \   __/|
 *  \ \   ___  \\ \  \_|/__\ \  \_|/__
 *   \ \  \\ \  \\ \  \_|\ \\ \  \_|\ \
 *    \ \__\\ \__\\ \_______\\ \_______\
 *     \|__| \|__| \|_______| \|_______|
 */
// const long long mod = 1000000007;
const long long mod = 998244353;

// watch this
// using ll = long long;
// typedef long long LL;
#define ll long long
#undef ll

const int mxN = 100010;

long long pow_mod(long long base, long long power) {
  long long res = 1;
  while (power) {
    if (power & 1)
      res = res * base % mod;
    base = base * base % mod;
    power >>= 1;
  }
  return res;
}

long long fact[mxN], inv[mxN];

void init() {
  fact[0] = fact[1] = inv[0] = inv[1] = 1;
  for (int i = 2; i < mxN; i++)
    fact[i] = i * fact[i - 1] % mod, inv[i] = pow_mod(fact[i], mod - 2);
}

long long NcR(long long N, long long R) {
  if (N < R)
    return 0;
  return (fact[N] * inv[R] % mod) * inv[N - R] % mod;
}

int main(int argc, char **argv) {
  // #ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // #endif

  init();

  int N;
  long long A, B, K;
  cin >> N >> A >> B >> K;
  long long res = 0;
  for (long long a = 0; a <= N; a++) {
    long long b = K - a * A;
    if (b % B)
      continue;
    b /= B;
    if (b < 0 || b > N)
      continue;
    res = (res + NcR(N, a) * NcR(N, b)) % mod;
  }
  cout << res << '\n';
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/rope>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
//
// template<class T> using Tree = tree<T, null_type, less<T>,
// rb_t_tag,t_order_statistics_node_update>;
/***********************************************/
/* Dear online judge:
 * I've read the problem, and tried to solve it.
 * Even if you don't accept my solution, you should respect my effort.
 * I hope my code compiles and gets accepted.
 *  ___  __     _______    _______
 * |\  \|\  \  |\  ___ \  |\  ___ \
 * \ \  \/  /|_\ \   __/| \ \   __/|
 *  \ \   ___  \\ \  \_|/__\ \  \_|/__
 *   \ \  \\ \  \\ \  \_|\ \\ \  \_|\ \
 *    \ \__\\ \__\\ \_______\\ \_______\
 *     \|__| \|__| \|_______| \|_______|
 */
// const long long mod = 1000000007;
const long long mod = 998244353;

// watch this
// using ll = long long;
// typedef long long LL;
#define ll long long
#undef ll

const int mxN = 300010;

long long pow_mod(long long base, long long power) {
  long long res = 1;
  while (power) {
    if (power & 1)
      res = res * base % mod;
    base = base * base % mod;
    power >>= 1;
  }
  return res;
}

long long fact[mxN], inv[mxN];

void init() {
  fact[0] = fact[1] = inv[0] = inv[1] = 1;
  for (int i = 2; i < mxN; i++)
    fact[i] = i * fact[i - 1] % mod, inv[i] = pow_mod(fact[i], mod - 2);
}

long long NcR(long long N, long long R) {
  if (N < R)
    return 0;
  return (fact[N] * inv[R] % mod) * inv[N - R] % mod;
}

int main(int argc, char **argv) {
  // #ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // #endif

  init();

  int N;
  long long A, B, K;
  cin >> N >> A >> B >> K;
  long long res = 0;
  for (long long a = 0; a <= N; a++) {
    long long b = K - a * A;
    if (b % B)
      continue;
    b /= B;
    if (b < 0 || b > N)
      continue;
    res = (res + NcR(N, a) * NcR(N, b)) % mod;
  }
  cout << res << '\n';
  return 0;
}