Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
double pi = 3.141592653589793;
ll mod = 998244353;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;

ll multiply(ll x, ll y) { return (x % mod) * (y % mod) % mod; }

ll power(ll x, ll y) {
  if (y == 0) {
    return 1;
  } else if (y == 1) {
    return x % mod;
  } else if (y % 2 == 0) {
    ll p = power(x, y / 2);
    return p * p % mod;
  } else {
    ll p = power(x, y / 2);
    return (p * p) % mod * (x % mod) % mod;
  }
}

ll divide(ll x, ll y) { return multiply(x, power(y, mod - 2)); }

ll frac[1000000];
ll invfrac[1000000];

void fracinvfrac(ll n) {
  frac[0] = 1;
  for (int i = 1; i <= n; i++) {
    frac[i] = multiply(frac[i - 1], i);
  }
  invfrac[n] = divide(1, frac[n]);
  for (int i = n - 1; i >= 0; i--) {
    invfrac[i] = multiply(invfrac[i + 1], i + 1);
  }
}

ll modcombi(ll x, ll y) {
  if (y == 0) {
    return 1;
  } else {
    return multiply(multiply(frac[x], invfrac[x - y]), invfrac[y]);
  }
}

int main() {
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  ll ans = 0;
  fracinvfrac(N);
  ll num = min(N, K / A);
  for (ll i = 0; i <= num; i++) {
    ll bres = K - A * i;
    if (bres % B == 0) {
      ll bnum = bres / B;
      ans = (ans + multiply(modcombi(N, i), modcombi(N, bnum))) % mod;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
double pi = 3.141592653589793;
ll mod = 998244353;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;

ll multiply(ll x, ll y) { return (x % mod) * (y % mod) % mod; }

ll power(ll x, ll y) {
  if (y == 0) {
    return 1;
  } else if (y == 1) {
    return x % mod;
  } else if (y % 2 == 0) {
    ll p = power(x, y / 2);
    return p * p % mod;
  } else {
    ll p = power(x, y / 2);
    return (p * p) % mod * (x % mod) % mod;
  }
}

ll divide(ll x, ll y) { return multiply(x, power(y, mod - 2)); }

ll frac[1000000];
ll invfrac[1000000];

void fracinvfrac(ll n) {
  frac[0] = 1;
  for (int i = 1; i <= n; i++) {
    frac[i] = multiply(frac[i - 1], i);
  }
  invfrac[n] = divide(1, frac[n]);
  for (int i = n - 1; i >= 0; i--) {
    invfrac[i] = multiply(invfrac[i + 1], i + 1);
  }
}

ll modcombi(ll x, ll y) {
  if (y == 0) {
    return 1;
  } else {
    return multiply(multiply(frac[x], invfrac[x - y]), invfrac[y]);
  }
}

int main() {
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  ll ans = 0;
  fracinvfrac(N);
  ll num = min(N, K / A);
  for (ll i = 0; i <= num; i++) {
    ll bres = K - A * i;
    if (bres % B == 0) {
      ll bnum = bres / B;
      if (bnum <= N) {
        ans = (ans + multiply(modcombi(N, i), modcombi(N, bnum))) % mod;
      }
    }
  }
  cout << ans << endl;
}