Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <cstdlib>
#include <deque> // deque
#include <iomanip>
#include <iostream>
#include <limits>
#include <map> // map
#include <numeric>
#include <queue> // queue, priority_queue
#include <set>   // set
#include <stack> // stack
#include <stdio.h>
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) { return max(max(a, b), c); }
ll Min(ll(a), ll(b), ll(c)) { return min(min(a, b), c); }

vector<ll> fact, factinv, inv;
ll mod = 998244353;
void prenCkModp(ll n) {
  fact.resize(n + 5);
  factinv.resize(n + 5);
  inv.resize(n + 5);
  fact.at(0) = fact.at(1) = 1;
  factinv.at(0) = factinv.at(1) = 1;
  inv.at(1) = 1;
  for (ll i = 2; i < n + 5; i++) {
    fact.at(i) = (fact.at(i - 1) * i) % mod;
    inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
    factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
  }
}
ll nCk(ll n, ll k) {
  return (fact.at(n) * ((factinv.at(k) * factinv.at(n - k)) % mod)) % mod;
}
int modPow(long long a, long long n, long long p) {
  if (n == 0)
    return 1; // 0乗にも対応する場合
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}

int main() {
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  prenCkModp(N + N);
  ll an = 0;
  rep(i, N + 1) {
    if ((K - A * i) % B == 0 && K - A * i >= 0) {
      an += nCk(N, i) * nCk(N, (K - A * i) / B);
      an %= mod;
    }
  }
  cout << an << endl;
}

Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <cstdlib>
#include <deque> // deque
#include <iomanip>
#include <iostream>
#include <limits>
#include <map> // map
#include <numeric>
#include <queue> // queue, priority_queue
#include <set>   // set
#include <stack> // stack
#include <stdio.h>
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) { return max(max(a, b), c); }
ll Min(ll(a), ll(b), ll(c)) { return min(min(a, b), c); }

vector<ll> fact, factinv, inv;
ll mod = 998244353;
void prenCkModp(ll n) {
  fact.resize(n + 5);
  factinv.resize(n + 5);
  inv.resize(n + 5);
  fact.at(0) = fact.at(1) = 1;
  factinv.at(0) = factinv.at(1) = 1;
  inv.at(1) = 1;
  for (ll i = 2; i < n + 5; i++) {
    fact.at(i) = (fact.at(i - 1) * i) % mod;
    inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
    factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
  }
}
ll nCk(ll n, ll k) {
  return (fact.at(n) * ((factinv.at(k) * factinv.at(n - k)) % mod)) % mod;
}
int modPow(long long a, long long n, long long p) {
  if (n == 0)
    return 1; // 0乗にも対応する場合
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}

int main() {
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  prenCkModp(N + N);
  ll an = 0;
  rep(i, N + 1) {
    if ((K - A * i) % B == 0 && K - A * i >= 0 && N >= (K - A * i) / B) {
      an += nCk(N, i) * nCk(N, (K - A * i) / B);
      an %= mod;
    }
  }
  cout << an << endl;
}