Problem p03332 (C++)

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define fr(x, v) for (auto &x : v)
#define pr(a, b) cout << a << " " << b << endl
#define pr1(a, b, c) cout << a << " " << b << " " << c << endl

typedef long long int ll;
typedef vector<ll> vi;

struct input {
  ll a;
};

const ll M = 998244353;
const ll N = 3e5 + 1;
template <class T> T gcd(const T a, const T b) {
  return (b ? gcd<T>(b, a % b) : a);
}

ll pw(ll x, ll p) {
  if (p == 0)
    return 1;
  ll t = pw(x, p / 2);
  if (p % 2 == 0)
    return (t * t) % M;
  else
    return (((t * t) % M) * x) % M;
}

int main() {

  fastio;

  ll n, m, i, j, k, temp, x, y, z, t, p;
  string S;
  ll a, b;
  cin >> n >> a >> b >> k;
  ll modi[n + 1];
  modi[0] = 1;
  modi[1] = 1;
  ll nf = 1;

  for (i = 2; i <= n; i++) {
    nf = (i * nf) % M;
    modi[i] = (modi[i - 1] * pw(i, M - 2)) % M;
  }
  // cout << nf*modi[n] << endl;
  ll ans = 0;
  for (i = 0; i <= n; i++) {
    t = (k - a * i);
    x = i;
    if (t >= 0 && t % b == 0) {
      y = (t) / b;
      // pr(x,y);
      ans += (((((nf * modi[x]) % M) * modi[n - x]) % M) *
              ((((nf * modi[y]) % M) * modi[n - y]) % M)) %
             M;
      ans %= M;
    }
  }

  cout << ans << endl;
  return 0;
}

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define fr(x, v) for (auto &x : v)
#define pr(a, b) cout << a << " " << b << endl
#define pr1(a, b, c) cout << a << " " << b << " " << c << endl

typedef long long int ll;
typedef vector<ll> vi;

struct input {
  ll a;
};

const ll M = 998244353;
const ll N = 3e5 + 1;
template <class T> T gcd(const T a, const T b) {
  return (b ? gcd<T>(b, a % b) : a);
}

ll pw(ll x, ll p) {
  if (p == 0)
    return 1;
  ll t = pw(x, p / 2);
  if (p % 2 == 0)
    return (t * t) % M;
  else
    return (((t * t) % M) * x) % M;
}

int main() {

  fastio;

  ll n, m, i, j, k, temp, x, y, z, t, p;
  string S;
  ll a, b;
  cin >> n >> a >> b >> k;
  ll modi[n + 1];
  modi[0] = 1;
  modi[1] = 1;
  ll nf = 1;

  for (i = 2; i <= n; i++) {
    nf = (i * nf) % M;
    modi[i] = (modi[i - 1] * pw(i, M - 2)) % M;
  }
  // cout << nf*modi[n] << endl;
  ll ans = 0;
  for (i = 0; i <= n; i++) {
    t = (k - a * i);
    x = i;
    if (t >= 0 && t % b == 0) {
      y = (t) / b;
      // pr(x,y);
      if (y <= n) {
        ans += (((((nf * modi[x]) % M) * modi[n - x]) % M) *
                ((((nf * modi[y]) % M) * modi[n - y]) % M)) %
               M;
        ans %= M;
      }
    }
  }

  cout << ans << endl;
  return 0;
}