Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i, a, b) for (ll i = a; i <= b; i++)
#define fast_io ios_base::sync_with_stdio(false)
#define pb push_back
#define fir first
#define sec second
#define tr(it, x) for (it = x.begin(); it != x.end(); it++)
#define mp make_pair
#define sz(x) (ll) x.size()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define double long double
#define accuracy ll precision = numeric_limits<double>::digits10
#define pll pair<ll, ll>
#define N 300005

ll n, aa, bb, k;
const ll pri = 998244353;
ll fac[N], invFac[N];

ll myPow(ll a, ll b) {
  ll ret = 1;
  while (b != 0) {
    if (b % 2 != 0) {
      ret = (ret * a) % pri;
    }
    b /= 2;
    a = (a * a) % pri;
  }
  return ret;
}

ll inv(ll a, ll b) { return myPow(a, b - 2); }

ll ncr(ll r, ll t) {
  ll ret = 1;
  ret *= fac[r];
  ret *= invFac[r - t];
  ret %= pri;
  ret *= invFac[t];
  ret %= pri;
  return ret;
}

void pre() {
  fac[0] = 1;
  invFac[0] = 1;
  fr(i, 1, N - 1) {
    fac[i] = (fac[i - 1] * i) % pri;
    invFac[i] = inv(fac[i], pri);
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast_io;
  accuracy;
  cin >> n >> aa >> bb >> k;
  pre();
  ll temp = __gcd(aa, bb);
  if (k % temp == 0) {
    ll ans = 0;
    fr(i, 0, n) {
      if ((k - (i * aa)) % bb == 0) {
        ll temp = (k - (i * aa));
        temp /= bb;
        if (temp < 0)
          break;
        ans += (ncr(n, i) * ncr(n, temp));
        ans %= pri;
      }
    }
    cout << ans << "\n";
  } else
    cout << 0 << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i, a, b) for (ll i = a; i <= b; i++)
#define fast_io ios_base::sync_with_stdio(false)
#define pb push_back
#define fir first
#define sec second
#define tr(it, x) for (it = x.begin(); it != x.end(); it++)
#define mp make_pair
#define sz(x) (ll) x.size()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define double long double
#define accuracy ll precision = numeric_limits<double>::digits10
#define pll pair<ll, ll>
#define N 300005

ll n, aa, bb, k;
const ll pri = 998244353;
ll fac[N], invFac[N];

ll myPow(ll a, ll b) {
  ll ret = 1;
  while (b != 0) {
    if (b % 2 != 0) {
      ret = (ret * a) % pri;
    }
    b /= 2;
    a = (a * a) % pri;
  }
  return ret;
}

ll inv(ll a, ll b) { return myPow(a, b - 2); }

ll ncr(ll r, ll t) {
  if (t > r)
    return 0;
  ll ret = 1;
  ret *= fac[r];
  ret *= invFac[r - t];
  ret %= pri;
  ret *= invFac[t];
  ret %= pri;
  return ret;
}

void pre() {
  fac[0] = 1;
  invFac[0] = 1;
  fr(i, 1, N - 1) {
    fac[i] = (fac[i - 1] * i) % pri;
    invFac[i] = inv(fac[i], pri);
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast_io;
  accuracy;
  cin >> n >> aa >> bb >> k;
  pre();
  ll temp = __gcd(aa, bb);
  if (k % temp == 0) {
    ll ans = 0;
    fr(i, 0, n) {
      if ((k - (i * aa)) % bb == 0) {
        ll temp = (k - (i * aa));
        temp /= bb;
        if (temp < 0)
          break;
        ans += (ncr(n, i) * ncr(n, temp));
        ans %= pri;
      }
    }
    cout << ans << "\n";
  } else
    cout << 0 << "\n";
}