Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 998244353

long long int fact[300001];
long long int factrev[300001];

void getfact(long long int n) {
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
  }
}

long long int tmp[300001][31];

void getfactrev(int n) {
  // cout << "hey" << endl;
  for (int i = 0; i <= n; i++) {
    tmp[i][0] = fact[i];
  }
  for (int i = 1; i < 31; i++) {
    for (int j = 0; j <= n; j++) {
      tmp[j][i] = (tmp[j][i - 1] * tmp[j][i - 1]) % mod;
    }
  }
  for (int i = 0; i <= n; i++) {
    factrev[i] = 1;
    long long int rest = mod - 2;
    for (int j = 30; j >= 0; j--) {
      long long int now = 1 << j;
      if (rest >= now) {
        rest -= now;
        factrev[i] = (factrev[i] * tmp[i][j]) % mod;
        // cout << i << " " << rest << " " << factrev[i] << " " << tmp[i][j] <<
        // endl;
      }
    }
  }
}

long long int comb(long long int n, long long int k) {
  // cout << "comb " << n << " " << k << endl;
  // cout << fact[n] << " " << factrev[k] << " " << factrev[n - k] << endl;
  return (fact[n] * ((factrev[k] * factrev[n - k]) % mod)) % mod;
}

int main() {
  long long int n, a, b, k;
  cin >> n >> a >> b >> k;
  getfact(n);
  getfactrev(n);
  long long int ans = 0;
  for (long long int p = 0; p <= n && a * p <= k; p++) {
    if ((k - a * p) % b > 0)
      continue;
    ans += (comb(n, p) * comb(n, (k - a * p) / b)) % mod;
    ans %= mod;
    // cout << p << " " << ans << endl;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 998244353

long long int fact[300001];
long long int factrev[300001];

void getfact(long long int n) {
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (i * fact[i - 1]) % mod;
  }
}

long long int tmp[300001][31];

void getfactrev(int n) {
  // cout << "hey" << endl;
  for (int i = 0; i <= n; i++) {
    tmp[i][0] = fact[i];
  }
  for (int i = 1; i < 31; i++) {
    for (int j = 0; j <= n; j++) {
      tmp[j][i] = (tmp[j][i - 1] * tmp[j][i - 1]) % mod;
    }
  }
  for (int i = 0; i <= n; i++) {
    factrev[i] = 1;
    long long int rest = mod - 2;
    for (int j = 30; j >= 0; j--) {
      long long int now = 1 << j;
      if (rest >= now) {
        rest -= now;
        factrev[i] = (factrev[i] * tmp[i][j]) % mod;
        // cout << i << " " << rest << " " << factrev[i] << " " << tmp[i][j] <<
        // endl;
      }
    }
  }
}

long long int comb(long long int n, long long int k) {
  // cout << "comb " << n << " " << k << endl;
  // cout << fact[n] << " " << factrev[k] << " " << factrev[n - k] << endl;
  return (fact[n] * ((factrev[k] * factrev[n - k]) % mod)) % mod;
}

int main() {
  long long int n, a, b, k;
  cin >> n >> a >> b >> k;
  getfact(n);
  getfactrev(n);
  long long int ans = 0;
  for (long long int p = 0; p <= n && a * p <= k; p++) {
    if ((k - a * p) % b > 0 || (k - a * p) / b > n)
      continue;
    ans += (comb(n, p) * comb(n, (k - a * p) / b)) % mod;
    ans %= mod;
    // cout << p << " " << ans << endl;
  }
  cout << ans << endl;
}