Problem p03332 (C++)

Buggy submission (status: Runtime Error):
// Template by [thunder_blade]
// IIIT ALLAHABAD
// includes :)

#include <bits/stdc++.h>
using namespace std;
#define TEST                                                                   \
  int test_case;                                                               \
  cin >> test_case;                                                            \
  while (test_case--)
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ll long long int
#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define pf(x) printf("%f\n", x)
#define ps(x) printf("%s\n", x)
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define sf(x) scanf("%f", &x)
#define ss(x) scanf("%s", x)
#define pis(x) cout << x << " " // printf("%d ",x)
#define pls(x) printf("%lld ", x)
#define pfs(x) printf("%f ", x)
#define pss(x) printf("%s ", x)
#define FOR(i, a, b) for (i = a; i < b; i++)
#define mod 998244353
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
ll power(ll a, ll n) {
  ll p = 1;
  while (n > 0) {
    if (n & 1) {
      p = ((p % mod) * (a % mod)) % mod;
    }
    n >>= 1;
    a = ((a % mod) * (a % mod)) % mod;
  }
  return p % mod;
}
ll inf = 1000;
const long long B = 100000007;
const long long C = 10010107;
const int N = 500005;
const int LN = 25;

ll fr[N], bk[N];

ll ncr(ll n, ll r) {
  ll tmp = (fr[n] * bk[r]) % mod;
  tmp *= bk[n - r];
  tmp %= mod;
  return tmp;
}

int main() {
  fr[0] = fr[1] = 1;
  bk[0] = bk[1] = 1;

  for (ll i = 2; i <= 300000; i++) {
    fr[i] = (fr[i - 1] * i) % mod;
    bk[i] = power(fr[i], mod - 2 * 1LL);
  }
  ll n, a, b, k;
  sl(n), sl(a), sl(b), sl(k);

  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll x = k - (a * i);
    if (x % b)
      continue;

    x /= b;
    if (x > n)
      continue;
    ll var = (ncr(n, i) * ncr(n, x)) % mod;
    ans += var;
    ans %= mod;
  }
  pl(ans);
}


Fixed submission:
// Template by [thunder_blade]
// IIIT ALLAHABAD
// includes :)

#include <bits/stdc++.h>
using namespace std;
#define TEST                                                                   \
  int test_case;                                                               \
  cin >> test_case;                                                            \
  while (test_case--)
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ll long long int
#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define pf(x) printf("%f\n", x)
#define ps(x) printf("%s\n", x)
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define sf(x) scanf("%f", &x)
#define ss(x) scanf("%s", x)
#define pis(x) cout << x << " " // printf("%d ",x)
#define pls(x) printf("%lld ", x)
#define pfs(x) printf("%f ", x)
#define pss(x) printf("%s ", x)
#define FOR(i, a, b) for (i = a; i < b; i++)
#define mod 998244353
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
ll power(ll a, ll n) {
  ll p = 1;
  while (n > 0) {
    if (n & 1) {
      p = ((p % mod) * (a % mod)) % mod;
    }
    n >>= 1;
    a = ((a % mod) * (a % mod)) % mod;
  }
  return p % mod;
}
ll inf = 1000;
const long long B = 100000007;
const long long C = 10010107;
const int N = 500005;
const int LN = 25;

ll fr[N], bk[N];

ll ncr(ll n, ll r) {
  ll tmp = (fr[n] * bk[r]) % mod;
  tmp *= bk[n - r];
  tmp %= mod;
  return tmp;
}

int main() {
  fr[0] = fr[1] = 1;
  bk[0] = bk[1] = 1;

  for (ll i = 2; i <= 300000; i++) {
    fr[i] = (fr[i - 1] * i) % mod;
    bk[i] = power(fr[i], mod - 2 * 1LL);
  }
  ll n, a, b, k;
  sl(n), sl(a), sl(b), sl(k);

  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll x = k - (a * i);
    if (x < 0 || (x % b))
      continue;

    x /= b;
    if (x > n)
      continue;
    ll var = (ncr(n, i) * ncr(n, x)) % mod;
    ans += var;
    ans %= mod;
  }
  pl(ans);
}