Problem p03332 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vi lnum;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (ll) V.size()
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979
#define fopen                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define FORR(i, b, a) for (ll i = (b)-1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A, value) memset(A, value, sizeof(A))
#define code first
#define index second

const ll mod = 998244353;
// const ll mod = 2 * (1000 * 1000 * 1000 + 7);
const ll MOD91 = 1e9 + 9;
const ll MOD12 = 1e12 + 39LL;
const ll MOD15 = 1e15 + 37LL;
const ll INF = 1000 * 1000 * 1000;
const ll LINF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000;
const ll base = 1e9;
const ll MAX = 2e5;
const double EPS = 1e-10;
const double EPS2 = 1e-9;
int fact[300001];
int mult(int a, int b) { return ((ll)a * b) % mod; }
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
ll N, A, B, K, X, Y;
ll gcdExtended(ll a, ll b, ll *x, ll *y);

// Function to find modulo inverse of a
ll modInverse(ll a, ll m) {
  ll x, y;
  ll g = gcdExtended(a, m, &x, &y);
  if (g != 1)
    cout << "Inverse doesn't exist";
  else {
    // m is added to handle negative x
    ll res = (x % m + m) % m;
    return res;
  }
}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  ll x1, y1; // To store results of recursive call
  ll gcd = gcdExtended(b % a, a, &x1, &y1);

  // Update x and y using results of recursive
  // call
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}
int C(int n, int k) {
  return mult(fact[n],
              mult(modInverse(fact[k], mod), modInverse(fact[n - k], mod)));
}
int main() {
  fact[0] = 1;
  for (int i = 1; i <= 300000; i++) {
    fact[i] = mult(fact[i - 1], i);
  }
  cin >> N >> A >> B >> K;
  int ans = 0;
  for (int i = 0; i <= N; i++) {
    ll lft = K - A * i;
    if (lft >= 0 && !(lft % B)) {
      int j = lft / B;
      ans = add(ans, mult(C(N, i), C(N, j)));
    }
  }

  cout << ans;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vi lnum;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (ll) V.size()
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979
#define fopen                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define FORR(i, b, a) for (ll i = (b)-1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define REPR(i, N) RFOR(i, N, 0)
#define FILL(A, value) memset(A, value, sizeof(A))
#define code first
#define index second

const ll mod = 998244353;
// const ll mod = 2 * (1000 * 1000 * 1000 + 7);
const ll MOD91 = 1e9 + 9;
const ll MOD12 = 1e12 + 39LL;
const ll MOD15 = 1e15 + 37LL;
const ll INF = 1000 * 1000 * 1000;
const ll LINF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000;
const ll base = 1e9;
const ll MAX = 2e5;
const double EPS = 1e-10;
const double EPS2 = 1e-9;
int fact[300001];
int mult(int a, int b) { return ((ll)a * b) % mod; }
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
ll N, A, B, K, X, Y;
ll gcdExtended(ll a, ll b, ll *x, ll *y);

// Function to find modulo inverse of a
ll modInverse(ll a, ll m) {
  ll x, y;
  ll g = gcdExtended(a, m, &x, &y);
  if (g != 1)
    cout << "Inverse doesn't exist";
  else {
    // m is added to handle negative x
    ll res = (x % m + m) % m;
    return res;
  }
}

// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll *x, ll *y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  ll x1, y1; // To store results of recursive call
  ll gcd = gcdExtended(b % a, a, &x1, &y1);

  // Update x and y using results of recursive
  // call
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}
int C(int n, int k) {
  return mult(fact[n],
              mult(modInverse(fact[k], mod), modInverse(fact[n - k], mod)));
}
int main() {
  fact[0] = 1;
  for (int i = 1; i <= 300000; i++) {
    fact[i] = mult(fact[i - 1], i);
  }
  cin >> N >> A >> B >> K;
  int ans = 0;
  for (int i = 0; i <= N; i++) {
    ll lft = K - A * i;
    if (lft >= 0 && !(lft % B)) {
      int j = lft / B;
      if (j <= N)
        ans = add(ans, mult(C(N, i), C(N, j)));
    }
  }

  cout << ans;
  return 0;
}