Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

ll mod = 998244353;
long long ruijo(long long k, ll m) {
  if (m == 0)
    return 1;
  if (m == 1)
    return k % mod;
  long long s = ruijo(k, m / 2);
  if (m % 2 == 0) {
    return (s * s) % mod;
  } else {
    long long ans;
    ans = (s * s) % mod;
    return (k * ans) % mod;
  }
}
int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  vector<long long> p(n + 2);
  p[0] = 1, p[1] = 1;
  vector<long long> inv(n + 2);
  for (long long i = 2; i < n + 2; i++) {
    p[i] = (p[i - 1] * i) % mod;
  }
  inv[n + 1] = ruijo(p[n + 1], mod - 2);
  for (long long i = n; i >= 0; i--) {
    inv[i] = ((i + 1) * inv[i + 1]) % mod;
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {

    if ((k - a * i) % b == 0) {
      ll s = (k - a * i) / b;
      if (s <= n) {
        ll tmp = p[n] * inv[i] % mod;
        tmp = tmp * inv[n - i] % mod;
        tmp = tmp * p[n] % mod;
        tmp = tmp * inv[s] % mod;
        tmp = tmp * inv[n - s] % mod;
        // cout << i << " " << s << " "<< tmp << endl;
        ans = (ans + tmp) % mod;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

ll mod = 998244353;
long long ruijo(long long k, ll m) {
  if (m == 0)
    return 1;
  if (m == 1)
    return k % mod;
  long long s = ruijo(k, m / 2);
  if (m % 2 == 0) {
    return (s * s) % mod;
  } else {
    long long ans;
    ans = (s * s) % mod;
    return (k * ans) % mod;
  }
}
int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  vector<long long> p(n + 2);
  p[0] = 1, p[1] = 1;
  vector<long long> inv(n + 2);
  for (long long i = 2; i < n + 2; i++) {
    p[i] = (p[i - 1] * i) % mod;
  }
  inv[n + 1] = ruijo(p[n + 1], mod - 2);
  for (long long i = n; i >= 0; i--) {
    inv[i] = ((i + 1) * inv[i + 1]) % mod;
  }
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (k - a * i < 0)
      continue;
    if ((k - a * i) % b == 0) {
      ll s = (k - a * i) / b;
      if (s <= n) {
        ll tmp = p[n] * inv[i] % mod;
        tmp = tmp * inv[n - i] % mod;
        tmp = tmp * p[n] % mod;
        tmp = tmp * inv[s] % mod;
        tmp = tmp * inv[n - s] % mod;
        // cout << i << " " << s << " "<< tmp << endl;
        ans = (ans + tmp) % mod;
      }
    }
  }
  cout << ans << endl;
  return 0;
}