prithivMLmods/Coder-Stat huggingface dataset:
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long i64;
using std::cin;
using std::cout;
using std::endl;

const int MOD = 998244353;

i64 mod_pow(i64 x, i64 n = MOD - 2, i64 mod = MOD) {
  i64 ret = 1;

  while (n) {
    if (n & 1)
      (ret *= x) %= MOD;
    (x *= x) %= MOD;
    n >>= 1;
  }

  return ret;
}

std::vector<i64> fact(1e6 + 1, 1);
i64 comb(int n, int r) {
  if (n < r)
    return 0;

  return fact[n] * mod_pow(fact[r]) % MOD * mod_pow(fact[n - r]) % MOD;
}

int main() {
  for (i64 i = 0; i < 1e6; i++)
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  i64 n, a, b, k;
  cin >> n >> a >> b >> k;

  i64 ans = 0;
  for (i64 i = 0; i <= n; i++) {
    i64 A = a * i;
    i64 B = k - A;
    if (B % b)
      continue;
    if (B / b > n)
      continue;

    ans += comb(n, i) * comb(n, B / b) % MOD;
    ans %= MOD;
  }

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


Fixed submission:
#include <bits/stdc++.h>
typedef long long i64;
using std::cin;
using std::cout;
using std::endl;

const int MOD = 998244353;

i64 mod_pow(i64 x, i64 n = MOD - 2, i64 mod = MOD) {
  i64 ret = 1;

  while (n) {
    if (n & 1)
      (ret *= x) %= MOD;
    (x *= x) %= MOD;
    n >>= 1;
  }

  return ret;
}

std::vector<i64> fact(1e6 + 1, 1);
i64 comb(int n, int r) {
  if (n < r)
    return 0;

  return fact[n] * mod_pow(fact[r]) % MOD * mod_pow(fact[n - r]) % MOD;
}

int main() {
  for (i64 i = 0; i < 1e6; i++)
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  i64 n, a, b, k;
  cin >> n >> a >> b >> k;

  i64 ans = 0;
  for (i64 i = 0; i <= n; i++) {
    i64 A = a * i;
    i64 B = k - A;
    if (B < 0 or B % b)
      continue;
    if (B / b > n)
      continue;

    ans += comb(n, i) * comb(n, B / b) % MOD;
    ans %= MOD;
  }

  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
const ll mo = 998244353;
const int MAX = 510000;
const int MOD = 998244353;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  COMinit();
  rep(i, n + 1) {
    if ((k - a * i) % b == 0 && (k - a * i) / b >= 0) {
      ll re = (k - a * i) / b;
      ll bl = i;
      if (re < bl)
        swap(re, bl);
      rep(j, bl + 1) {
        ans += (COM(n, re - j) * COM(n - re + j, bl - j) % mo) *
               COM(n - re - bl + j + j, j);
        ans %= mo;
      }
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
const ll mo = 998244353;
const int MAX = 510000;
const int MOD = 998244353;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  ll n, a, b, k;
  cin >> n >> a >> b >> k;
  COMinit();
  rep(i, n + 1) {
    if ((k - a * i) % b == 0 && (k - a * i) / b >= 0) {
      ll re = (k - a * i) / b;
      ll bl = i;
      ans += COM(n, re) * COM(n, bl);
      ans %= mo;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int oo = 1e9;
const int LOGN = 18;
const int MAX_VAL = 10000000;
const int MAX_N = 100001;
const int MOD = 998244353;

ll N, A, B, K;

ll res = 0;
ll fact[MAX_N];
ll inv[MAX_N];
ll pw(ll a, ll b) {
  if (!b)
    return 1ll;
  ll res = pw(a, b / 2);
  res = (res * res) % MOD;
  if (b & 1)
    return (res * a) % MOD;
  return res;
}
ll nck(ll n, ll k) { return ((fact[n] * inv[k]) % MOD * inv[n - k]) % MOD; }
int main() {
  fact[0] = 1;
  inv[0] = pw(fact[0], MOD - 2);
  for (int i = 1; i < MAX_N; ++i) {
    fact[i] = (i * fact[i - 1]) % MOD;
    inv[i] = pw(fact[i], MOD - 2);
  }
  cin >> N >> A >> B >> K;
  ll all = 0;
  for (int b = 0; b <= N; ++b) {
    ll r = (K - b * B) / A;
    if (r * A + b * B == K && r >= 0 && r <= N) {
      all = (all + nck(N, r) * nck(N, b)) % MOD;
    }
  }
  cout << all << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
const int oo = 1e9;
const int LOGN = 18;
const int MAX_VAL = 10000000;
const int MAX_N = 400001;
const int MOD = 998244353;

ll N, A, B, K;

ll res = 0;
ll fact[MAX_N];
ll inv[MAX_N];
ll pw(ll a, ll b) {
  if (!b)
    return 1ll;
  ll res = pw(a, b / 2);
  res = (res * res) % MOD;
  if (b & 1)
    return (res * a) % MOD;
  return res;
}
ll nck(ll n, ll k) { return ((fact[n] * inv[k]) % MOD * inv[n - k]) % MOD; }
int main() {
  fact[0] = 1;
  inv[0] = pw(fact[0], MOD - 2);
  for (int i = 1; i < MAX_N; ++i) {
    fact[i] = (i * fact[i - 1]) % MOD;
    inv[i] = pw(fact[i], MOD - 2);
  }
  cin >> N >> A >> B >> K;
  ll all = 0;
  for (int b = 0; b <= N; ++b) {
    ll r = (K - b * B) / A;
    if (r * A + b * B == K && r >= 0 && r <= N) {
      all = (all + nck(N, r) * nck(N, b)) % MOD;
    }
  }
  cout << all << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
// In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
const bool DEBUG = false;
#define cerr                                                                   \
  if (DEBUG)                                                                   \
  cerr
using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long long maxn = 3e5 + 5;
const long long INF = 1e18 + 1;
const long long M = 998244353;
const int lg = 21;

ll n, a, b, k, ans, f[maxn], rf[maxn];

ll POW(ll a, ll b) {
  if (b == 0)
    return 1;
  ll p = POW(a, b >> 1);
  p = p * p % M;
  if (b & 1)
    p = p * a % M;
  return p;
}

ll C(ll n, ll k) {
  if (k == 0 or k == n)
    return 1;
  return f[n] * rf[k] % M * rf[n - k] % M;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> a >> b >> k;
  f[0] = rf[0] = 1;
  for (int i = 1; i < maxn; i++)
    f[i] = f[i - 1] * i % M, rf[i] = rf[i - 1] * POW(i, M - 2) % M;
  for (int i = 0; i <= n; i++) {
    ll x = i * a;
    if (x <= k and (k - x) % b == 0) {
      ll t = (k - x) / b;
      ans = (ans + C(n, i) * C(n, t)) % M;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
const bool DEBUG = false;
#define cerr                                                                   \
  if (DEBUG)                                                                   \
  cerr
using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long long maxn = 3e5 + 5;
const long long INF = 1e18 + 1;
const long long M = 998244353;
const int lg = 21;

ll n, a, b, k, ans, f[maxn], rf[maxn];

ll POW(ll a, ll b) {
  if (b == 0)
    return 1;
  ll p = POW(a, b >> 1);
  p = p * p % M;
  if (b & 1)
    p = p * a % M;
  return p;
}

ll C(ll n, ll k) {
  if (k == 0 or k == n)
    return 1;
  return f[n] * rf[k] % M * rf[n - k] % M;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> a >> b >> k;
  f[0] = rf[0] = 1;
  for (int i = 1; i < maxn; i++)
    f[i] = f[i - 1] * i % M, rf[i] = rf[i - 1] * POW(i, M - 2) % M;
  for (int i = 0; i <= n; i++) {
    ll x = i * a;
    if (x <= k and (k - x) % b == 0 and (k - x) / b <= n) {
      ll t = (k - x) / b;
      ans = (ans + C(n, i) * C(n, t)) % M;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

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

#define int long long

const int MAXN = 2e5;
const int MOD = 998244353;

int fac[MAXN + 100];
int inv[MAXN + 100];

int pow(int base, int k, int mod) {
  int ret = 1;
  while (k) {
    if (k & 1)
      ret = (ret * base) % mod;
    base = (base * base) % mod, k >>= 1;
  }
  return ret;
}

void init() {
  fac[0] = fac[1] = inv[0] = 1;
  for (int i = 2; i <= MAXN; i++)
    fac[i] = (fac[i - 1] * i) % MOD;
  for (int i = 1; i <= MAXN; i++)
    inv[i] = pow(fac[i], MOD - 2, MOD);
  // for (int i = 1; i <= 10; i++)
  //     printf("inv(%lld!) is %lld, fac(%lld) is %lld\n", i, inv[i], i,
  //     fac[i]);
}

int C(int n, int m, int mod) {
  if (m < n)
    return 0;
  // printf("C(%lld, %lld) is %lld\n", n, m,
  //        (inv[n] * ((inv[m - n] * fac[m]) % mod)) % mod);
  return (inv[n] * ((inv[m - n] * fac[m]) % mod)) % mod;
}

signed main() {
  init();
  int n, a, b, k;
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);

  int ans = 0;

  int cnt_b;
  for (int cnt_a = 0; cnt_a <= n; cnt_a++) {
    cnt_b = (k - cnt_a * a) / b;
    if (cnt_b * b + cnt_a * a == k && cnt_b <= n && cnt_b >= 0)
      ans = (ans + C(cnt_a, n, MOD) * C(cnt_b, n, MOD)) % MOD;
  }

  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>

#define int long long

const int MAXN = 4e5;
const int MOD = 998244353;

int fac[MAXN + 100];
int inv[MAXN + 100];

int pow(int base, int k, int mod) {
  int ret = 1;
  while (k) {
    if (k & 1)
      ret = (ret * base) % mod;
    base = (base * base) % mod, k >>= 1;
  }
  return ret;
}

void init() {
  fac[0] = fac[1] = inv[0] = 1;
  for (int i = 2; i <= MAXN; i++)
    fac[i] = (fac[i - 1] * i) % MOD;
  for (int i = 1; i <= MAXN; i++)
    inv[i] = pow(fac[i], MOD - 2, MOD);
  // for (int i = 1; i <= 10; i++)
  //     printf("inv(%lld!) is %lld, fac(%lld) is %lld\n", i, inv[i], i,
  //     fac[i]);
}

int C(int n, int m, int mod) {
  if (m < n)
    return 0;
  // printf("C(%lld, %lld) is %lld\n", n, m,
  //        (inv[n] * ((inv[m - n] * fac[m]) % mod)) % mod);
  return (inv[n] * ((inv[m - n] * fac[m]) % mod)) % mod;
}

signed main() {
  init();
  int n, a, b, k;
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);

  int ans = 0;

  int cnt_b;
  for (int cnt_a = 0; cnt_a <= n; cnt_a++) {
    cnt_b = (k - cnt_a * a) / b;
    if (cnt_b * b + cnt_a * a == k && cnt_b <= n && cnt_b >= 0)
      ans = (ans + C(cnt_a, n, MOD) * C(cnt_b, n, MOD)) % MOD;
  }

  printf("%lld\n", ans);
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//---------------------------------------------------------------------------------------------------
ll MOD = 998244353;
ll f[301010], rf[301010];
ll inv(ll x) {
  ll res = 1;
  ll k = MOD - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % MOD;
    y = (y * y) % MOD;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  for (int i = 1; i < 301010; i++)
    f[i] = (f[i - 1] * i) % MOD;
  for (int i = 0; i < 301010; i++)
    rf[i] = inv(f[i]);
}
ll C(int n, int k) {
  if (n < k)
    return 0;
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % MOD;

  return (a * bc) % MOD;
}
//---------------------------------------------------------------------------------------------------

void add(ll &a, ll b) { a = (a + b) % MOD; }

int main() {
  init();
  ll n, a, b, k;
  cin >> n >> a >> b >> k;

  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    ll v = k - a * i;
    if (v % b != 0)
      continue;
    ll num = v / b;
    add(ans, C(n, i) * C(n, num));
  }
  cout << (ans + MOD) % MOD << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//---------------------------------------------------------------------------------------------------
ll MOD = 998244353;
ll f[301010], rf[301010];
ll inv(ll x) {
  ll res = 1;
  ll k = MOD - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % MOD;
    y = (y * y) % MOD;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  for (int i = 1; i < 301010; i++)
    f[i] = (f[i - 1] * i) % MOD;
  for (int i = 0; i < 301010; i++)
    rf[i] = inv(f[i]);
}
ll C(int n, int k) {
  if (n < k)
    return 0;
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % MOD;

  return (a * bc) % MOD;
}
//---------------------------------------------------------------------------------------------------

void add(ll &a, ll b) { a = (a + b) % MOD; }

int main() {
  init();
  ll n, a, b, k;
  cin >> n >> a >> b >> k;

  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    ll v = k - a * i;
    if (v < 0 || v % b != 0)
      continue;
    ll num = v / b;
    add(ans, C(n, i) * C(n, num));
  }
  cout << (ans + MOD) % MOD << endl;
}
Problem p03332 (C++)

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

#define llong long long
#define ii pair<int, int>
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i, n) for (int i = -1; ++i < n;)
#define rep1(i, n) for (int i = 0; i++ < n;)

#define rem 998244353
#define maxn 301010
llong fac[maxn];

llong mpow(llong u, llong m) {
  llong ans = 1;
  for (; m > 0; m >>= 1) {
    if (m & 1)
      (ans *= u) %= rem;
    (u *= u) %= rem;
  }
  return ans;
}

llong comp(llong n, llong k) {
  return fac[n] * (mpow(fac[k] * fac[n - k] % rem, rem - 2)) % rem;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  llong n, a, b, k;
  fac[0] = 1;
  rep1(i, maxn - 1)(fac[i] = fac[i - 1] * i) %= rem;
  cin >> n >> a >> b >> k;
  llong ans = 0;
  rep(i, n + 1) {
    if ((k - i * a) % b)
      continue;
    llong f = (k - i * a) / b;
    if (f > n)
      continue;
    // clog << i << ' ' << f << endl;
    (ans += comp(n, i) * comp(n, f) % rem) %= rem;
  }
  cout << ans;

  return 0;
}


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

#define llong long long
#define ii pair<int, int>
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i, n) for (int i = -1; ++i < n;)
#define rep1(i, n) for (int i = 0; i++ < n;)

#define rem 998244353
#define maxn 301010
llong fac[maxn];

llong mpow(llong u, llong m) {
  llong ans = 1;
  for (; m > 0; m >>= 1) {
    if (m & 1)
      (ans *= u) %= rem;
    (u *= u) %= rem;
  }
  return ans;
}

llong comp(llong n, llong k) {
  return fac[n] * (mpow(fac[k] * fac[n - k] % rem, rem - 2)) % rem;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  llong n, a, b, k;
  fac[0] = 1;
  rep1(i, maxn - 1)(fac[i] = fac[i - 1] * i) %= rem;
  cin >> n >> a >> b >> k;
  llong ans = 0;
  rep(i, n + 1) {
    if (i * a > k)
      break;
    if ((k - i * a) % b)
      continue;
    llong f = (k - i * a) / b;
    if (f > n)
      continue;
    // clog << i << ' ' << f << endl;
    (ans += comp(n, i) * comp(n, f) % rem) %= rem;
  }
  cout << ans;

  return 0;
}
Problem p03332 (C++)

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

long long inv[MAX_N];
long long factorial[MAX_N];
long long inv_factorial[MAX_N];

void getInv() {
  for (int i = 1; i < MAX_N; i++) {
    if (i == 1)
      inv[i] = 1;
    else {
      inv[i] = (mod - (mod / i) * inv[mod % i]) % mod;
      if (inv[i] < 0)
        inv[i] += mod;
    }
  }
}
void getFactorial() {
  factorial[0] = 1;
  inv_factorial[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    factorial[i] = factorial[i - 1] * i;
    factorial[i] %= mod;
    inv_factorial[i] = inv_factorial[i - 1] * inv[i];
    inv_factorial[i] %= mod;
  }
}

long long combination(int n, int r) {
  long long ret = factorial[n] * inv_factorial[r];
  ret %= mod;
  ret *= inv_factorial[n - r];
  return ret % mod;
}

int main() {
  getInv();
  getFactorial();
  long long n, a, b, k;
  cin >> n >> a >> b >> k;
  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    long long u = k - a * i;
    if (u % b != 0)
      continue;
    u /= b;
    if (u > n)
      continue;
    long long tmp = combination(n, i) * combination(n, u);
    tmp %= mod;
    ans += tmp;
    ans %= mod;
    // cout << i-j << " " << j << " " << u-j << " " << tmp << endl;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define MAX_N 300010

long long inv[MAX_N];
long long factorial[MAX_N];
long long inv_factorial[MAX_N];

void getInv() {
  for (int i = 1; i < MAX_N; i++) {
    if (i == 1)
      inv[i] = 1;
    else {
      inv[i] = (mod - (mod / i) * inv[mod % i]) % mod;
      if (inv[i] < 0)
        inv[i] += mod;
    }
  }
}
void getFactorial() {
  factorial[0] = 1;
  inv_factorial[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    factorial[i] = factorial[i - 1] * i;
    factorial[i] %= mod;
    inv_factorial[i] = inv_factorial[i - 1] * inv[i];
    inv_factorial[i] %= mod;
  }
}

long long combination(int n, int r) {
  long long ret = factorial[n] * inv_factorial[r];
  ret %= mod;
  ret *= inv_factorial[n - r];
  return ret % mod;
}

int main() {
  getInv();
  getFactorial();
  long long n, a, b, k;
  cin >> n >> a >> b >> k;
  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    long long u = k - a * i;
    if (u < 0)
      break;
    if (u % b != 0)
      continue;
    u /= b;
    if (u > n)
      continue;
    long long tmp = combination(n, i) * combination(n, u);
    tmp %= mod;
    ans += tmp;
    ans %= mod;
    // cout << i-j << " " << j << " " << u-j << " " << tmp << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 998244353;
ll fact[3 * (ll)1e5 + 1] = {1};
ll po(ll a, ll b) {
  return b ? (b & 1 ? a : 1) * po(a * a % MOD, b / 2) % MOD : 1;
}
ll comb(ll n, ll k) {
  return fact[n] * po(fact[k] * fact[n - k] % MOD, MOD - 2) % MOD;
}
int main() {
  for (ll i = 1; i <= 3 * 1e5; i++)
    fact[i] = i * fact[i - 1] % MOD;
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  ll ans = 0;
  for (ll a = 0; a <= N; a++) {
    if ((K - a * A) % B)
      continue;
    ll b = (K - a * A) / B;
    if (max(a, b) > N)
      continue;
    ll add = comb(N, a) * comb(N, b);
    ans = (ans + add) % MOD;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 998244353;
ll fact[3 * (ll)1e5 + 1] = {1};
ll po(ll a, ll b) {
  return b ? (b & 1 ? a : 1) * po(a * a % MOD, b / 2) % MOD : 1;
}
ll comb(ll n, ll k) {
  return fact[n] * po(fact[k] * fact[n - k] % MOD, MOD - 2) % MOD;
}
int main() {
  for (ll i = 1; i <= 3 * 1e5; i++)
    fact[i] = i * fact[i - 1] % MOD;
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  ll ans = 0;
  for (ll a = 0; a <= N; a++) {
    if (K - a * A < 0)
      continue;
    if ((K - a * A) % B)
      continue;
    ll b = (K - a * A) / B;
    if (max(a, b) > N)
      continue;
    ll add = comb(N, a) * comb(N, b);
    ans = (ans + add) % MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<ll> v_ll;
typedef vector<vector<ll>> vv_ll;
typedef vector<pair<ll, ll>> vp_ll;
typedef pair<ll, ll> p_ll;

ll mod_inv(ll x, ll n) {
  x %= n;
  if (x < 0)
    x += n;
  ll y = x, z = n - 2;
  ll ret = 1;
  while (z > 0) {
    if (z % 2 == 1) {
      ret *= y;
      ret %= n;
    }
    y *= y;
    y %= n;
    z /= 2;
  }
  return ret;
}

ll N, A, B, K;
ll MOD;
int main() {
  MOD = 998244353;
  cin >> N >> A >> B >> K;
  v_ll NCK(N + 1, 0);
  NCK[0] = 1;
  for (ll i = 1; i <= N; i++) {
    NCK[i] = (NCK[i - 1] * (N + 1 - i)) % MOD;
    NCK[i] *= mod_inv(i, MOD);
    NCK[i] %= MOD;
  }
  /*
  for (ll i=0;i<=N;i++){
    cout << NCK[i] << endl;
  }
  */
  ll ans = 0;
  ll a = 0, b = 0;
  for (ll i = 0; i <= N; i++) {
    if ((K - A * i) % B == 0 && (K - A * i) / B <= N) {
      a = i;
      b = (K - A * i) / B;
      ans += (NCK[a] * NCK[b]) % MOD;
      ans %= MOD;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<ll> v_ll;
typedef vector<vector<ll>> vv_ll;
typedef vector<pair<ll, ll>> vp_ll;
typedef pair<ll, ll> p_ll;

ll mod_inv(ll x, ll n) {
  x %= n;
  if (x < 0)
    x += n;
  ll y = x, z = n - 2;
  ll ret = 1;
  while (z > 0) {
    if (z % 2 == 1) {
      ret *= y;
      ret %= n;
    }
    y *= y;
    y %= n;
    z /= 2;
  }
  return ret;
}

ll N, A, B, K;
ll MOD;
int main() {
  MOD = 998244353;
  cin >> N >> A >> B >> K;
  v_ll NCK(N + 1, 0);
  NCK[0] = 1;
  for (ll i = 1; i <= N; i++) {
    NCK[i] = (NCK[i - 1] * (N + 1 - i)) % MOD;
    NCK[i] *= mod_inv(i, MOD);
    NCK[i] %= MOD;
  }
  /*
  for (ll i=0;i<=N;i++){
    cout << NCK[i] << endl;
  }
  */
  ll ans = 0;
  ll a = 0, b = 0;
  for (ll i = 0; i <= N; i++) {
    if ((K >= A * i) && (K - A * i) % B == 0 && (K - A * i) / B <= N) {
      a = i;
      b = (K - A * i) / B;
      ans += (NCK[a] * NCK[b]) % MOD;
      ans %= MOD;
    }
  }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long p = 998244353;
long long fac[333333], inv[333333], fi[333333];
long long c(long long x, long long y) {
  return fac[x] * fi[y] % p * fi[x - y] % p;
}
int main() {
  long long n, a, b, k, ans = 0;
  long long i, j;
  cin >> n >> a >> b >> k;
  fac[0] = fac[1] = inv[0] = inv[1] = fi[0] = fi[1] = 1;
  for (i = 2; i <= 323232; i++) {
    fac[i] = fac[i - 1] * i % p;
    inv[i] = (p - p / i) * inv[p % i] % p;
    fi[i] = fi[i - 1] * inv[i] % p;
  }
  for (i = 0; i <= n; i++) {
    j = (k - i * a) / b;
    if (i * a + j * b != k || j > n)
      continue;
    // cout<<c(n,i)<<' '<<c(n,j)<<endl;
    ans += c(n, i) * c(n, j) % p;
    ans %= p;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long p = 998244353;
long long fac[333333], inv[333333], fi[333333];
long long c(long long x, long long y) {
  return fac[x] * fi[y] % p * fi[x - y] % p;
}
int main() {
  long long n, a, b, k, ans = 0;
  long long i, j;
  cin >> n >> a >> b >> k;
  fac[0] = fac[1] = inv[0] = inv[1] = fi[0] = fi[1] = 1;
  for (i = 2; i <= 323232; i++) {
    fac[i] = fac[i - 1] * i % p;
    inv[i] = (p - p / i) * inv[p % i] % p;
    fi[i] = fi[i - 1] * inv[i] % p;
  }
  for (i = 0; i <= n; i++) {
    j = (k - i * a) / b;
    if (i * a + j * b != k || j > n || j < 0)
      continue;
    // cout<<c(n,i)<<' '<<c(n,j)<<endl;
    ans += c(n, i) * c(n, j) % p;
    ans %= p;
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
long long n, a, b, c, k, ans;
long long fac[300005], rev[300005];
long long power(long long x, long long y, long long mod) {
  if (y == 0) {
    return 1;
  } else {
    long long dm = power(x, y / 2, mod);
    if (y % 2 == 1) {
      return ((dm * dm) % mod * x) % mod;
    } else {
      return (dm * dm) % mod;
    }
  }
}
long long inverse_mod(long long a, long long mod) {
  return power(a, mod - 2, mod);
}
long long C(int k, int n) {
  if (k > n) {
    return 0;
  } else {
    return ((fac[n] * rev[k] % mod) * rev[n - k]) % mod;
  }
}
int main() {
  cin >> n >> a >> b >> k;
  fac[0] = 1;
  rev[0] = inverse_mod(fac[0], mod);
  for (int i = 1; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    rev[i] = inverse_mod(fac[i], mod) % mod;
  }
  if (k == 0) {
    cout << "1";
  } else {
    for (long long x = 1; x <= k / a && x <= n; x++) {
      long long tmp = k - a * x;
      if (tmp % b == 0) {
        long long y = tmp / b;
        if (max(x, y) <= n) {
          for (int z = min(x, y); x + y - z <= n && z >= 0; z--) {
            ans = (ans + ((C(x - z, n) % mod) * (C(y - z, n - x + z) % mod) %
                          mod * (C(z, n - x - y + 2 * z) % mod)) %
                             mod) %
                  mod;
          }
        }
      }
    }
    cout << ans;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
long long n, a, b, c, k, ans;
long long fac[300005], rev[300005];
long long power(long long x, long long y, long long mod) {
  if (y == 0) {
    return 1;
  } else {
    long long dm = power(x, y / 2, mod);
    if (y % 2 == 1) {
      return ((dm * dm) % mod * x) % mod;
    } else {
      return (dm * dm) % mod;
    }
  }
}
long long inverse_mod(long long a, long long mod) {
  return power(a, mod - 2, mod);
}
long long C(int k, int n) {
  if (k > n) {
    return 0;
  } else {
    return ((fac[n] * rev[k] % mod) * rev[n - k]) % mod;
  }
}
int main() {
  cin >> n >> a >> b >> k;
  fac[0] = 1;
  rev[0] = inverse_mod(fac[0], mod);
  for (int i = 1; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    rev[i] = inverse_mod(fac[i], mod) % mod;
  }
  if (k == 0) {
    cout << "1";
  } else {
    for (long long x = 1; x <= k / a && x <= n; x++) {
      long long tmp = k - a * x;
      if (tmp % b == 0) {
        long long y = tmp / b;
        ans = (ans + (C(x, n) % mod) * (C(y, n) % mod) % mod) % mod;
      }
    }
    cout << ans;
  }
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

ll frac[300005], shita[300005];

void prepare() {
  ll h = mod1 - 2;
  ll two;
  for (int i = 0; i < 300005; ++i)
    shita[i] = 1;
  ll tmp[300005];
  while (h > 0) {
    two = 1;
    for (int i = 0; i < 300005; ++i)
      tmp[i] = frac[i];
    while (2 * two < h) {
      two *= 2;
      for (int i = 0; i < 300005; ++i) {
        tmp[i] *= tmp[i];
        tmp[i] %= mod1;
      }
    }
    h -= two;
    for (int i = 0; i < 300005; ++i) {
      shita[i] *= tmp[i];
      shita[i] %= mod1;
    }
  }
}

ll comb(ll a, ll b) { return frac[a] * shita[a - b] % mod1 * shita[b] % mod1; }

int main() {
  frac[0] = 1;
  for (ll i = 1; i < 300005; ++i) {
    frac[i] = frac[i - 1] * i % mod1;
  }
  prepare();
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  ll ans = 0;
  for (ll x = 0; x <= N; ++x) {
    if (K - A * x < 0)
      continue;
    if ((K - A * x) % B != 0)
      continue;
    ans += comb(N, x) * comb(N, (K - A * x) / B) % mod1;
    ans %= mod1;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

ll frac[300005], shita[300005];

void prepare() {
  ll h = mod1 - 2;
  ll two;
  for (int i = 0; i < 300005; ++i)
    shita[i] = 1;
  ll tmp[300005];
  while (h > 0) {
    two = 1;
    for (int i = 0; i < 300005; ++i)
      tmp[i] = frac[i];
    while (2 * two < h) {
      two *= 2;
      for (int i = 0; i < 300005; ++i) {
        tmp[i] *= tmp[i];
        tmp[i] %= mod1;
      }
    }
    h -= two;
    for (int i = 0; i < 300005; ++i) {
      shita[i] *= tmp[i];
      shita[i] %= mod1;
    }
  }
}

ll comb(ll a, ll b) { return frac[a] * shita[a - b] % mod1 * shita[b] % mod1; }

int main() {
  frac[0] = 1;
  for (ll i = 1; i < 300005; ++i) {
    frac[i] = frac[i - 1] * i % mod1;
  }
  prepare();
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  ll ans = 0;
  for (ll x = 0; x <= N; ++x) {
    if (K - A * x < 0)
      continue;
    if ((K - A * x) % B != 0)
      continue;
    if ((K - A * x) / B > N)
      continue;
    ans += comb(N, x) * comb(N, (K - A * x) / B) % mod1;
    ans %= mod1;
  }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

ll frac[300005];

ll comb(ll a, ll b) {
  ll ue = frac[a];
  ll shita1 = 1, shita2 = 1;
  ll tmpshita1, tmpshita2, two, h;
  h = mod - 2;
  while (h > 0) {
    two = 1;
    tmpshita1 = frac[b];
    tmpshita2 = frac[a - b];
    while (2 * two < h) {
      two *= 2;
      tmpshita1 *= tmpshita1;
      tmpshita2 *= tmpshita2;
      tmpshita1 %= mod;
      tmpshita2 %= mod;
    }
    h -= two;
    shita1 *= tmpshita1;
    shita2 *= tmpshita2;
    shita1 %= mod;
    shita2 %= mod;
  }
  return ue * shita1 % mod * shita2 % mod;
}

int main() {
  frac[0] = 1;
  for (ll i = 1; i < 300005; ++i) {
    frac[i] = frac[i - 1] * i % mod;
  }
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  ll ans = 0;
  for (ll x = 0; x <= N; ++x) {
    if ((K - A * x) % B == 0) {
      if ((K - A * x) / B > N)
        continue;
      ans += comb(N, x) * comb(N, (K - A * x) / B) % mod;
      ans %= mod;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

ll frac[300005];

ll comb(ll a, ll b) {
  ll ue = frac[a];
  ll shita1 = 1, shita2 = 1;
  ll tmpshita1, tmpshita2, two, h;
  h = mod - 2;
  while (h > 0) {
    two = 1;
    tmpshita1 = frac[b];
    tmpshita2 = frac[a - b];
    while (2 * two < h) {
      two *= 2;
      tmpshita1 *= tmpshita1;
      tmpshita2 *= tmpshita2;
      tmpshita1 %= mod;
      tmpshita2 %= mod;
    }
    h -= two;
    shita1 *= tmpshita1;
    shita2 *= tmpshita2;
    shita1 %= mod;
    shita2 %= mod;
  }
  return ue * shita1 % mod * shita2 % mod;
}

int main() {
  frac[0] = 1;
  for (ll i = 1; i < 300005; ++i) {
    frac[i] = frac[i - 1] * i % mod;
  }
  ll N, A, B, K;
  cin >> N >> A >> B >> K;
  ll ans = 0;
  for (ll x = 0; x <= N; ++x) {
    if (K < A * x)
      continue;
    if ((K - A * x) % B == 0) {
      if ((K - A * x) / B > N)
        continue;
      ans += comb(N, x) * comb(N, (K - A * x) / B) % mod;
      ans %= mod;
    }
  }
  cout << ans << endl;
}
Problem p03332 (C++)

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

const int maxn = 300005;
const long long mod = 998244353;

long long n, a, b, k, ans;
long long fact[maxn];

long long add_mod(long long x, long long y) { return (x + y) % mod; }

long long mul_mod(long long x, long long y) { return (x * y) % mod; }

long long pow_mod(long long x, long long y) {
  long long res = 1, t = x;
  while (y) {
    if (y & 1)
      res = mul_mod(res, t);
    t = mul_mod(t, t);
    y >>= 1;
  }
  return res;
}

long long div_mod(long long x, long long y) {
  return mul_mod(x, pow_mod(y, mod - 2));
}

long long ncr_mod(long long x, long long y) {
  return div_mod(fact[x], mul_mod(fact[x - y], fact[y]));
}

void precalc() {
  fact[0] = 1;
  for (long long i = 1; i < maxn; ++i)
    fact[i] = mul_mod(i, fact[i - 1]);
}

signed main() {
  precalc();
  scanf("%lld %lld %lld %lld", &n, &a, &b, &k);
  for (long long i = 0; i <= n; ++i) {
    if ((k - i * a) >= 0 && (k - i * a) % b == 0 && 0 <= (k - i * a) / b &&
        (k - i * a) % b <= n)
      ans = add_mod(ans, mul_mod(ncr_mod(n, i), ncr_mod(n, (k - i * a) / b)));
  }
  printf("%lld\n", ans);
}


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

const int maxn = 300005;
const long long mod = 998244353;

long long n, a, b, k, ans;
long long fact[maxn];

long long add_mod(long long x, long long y) { return (x + y) % mod; }

long long mul_mod(long long x, long long y) { return (x * y) % mod; }

long long pow_mod(long long x, long long y) {
  long long res = 1, t = x;
  while (y) {
    if (y & 1)
      res = mul_mod(res, t);
    t = mul_mod(t, t);
    y >>= 1;
  }
  return res;
}

long long div_mod(long long x, long long y) {
  return mul_mod(x, pow_mod(y, mod - 2));
}

long long ncr_mod(long long x, long long y) {
  return div_mod(fact[x], mul_mod(fact[x - y], fact[y]));
}

void precalc() {
  fact[0] = 1;
  for (long long i = 1; i < maxn; ++i)
    fact[i] = mul_mod(i, fact[i - 1]);
}

signed main() {
  precalc();
  scanf("%lld %lld %lld %lld", &n, &a, &b, &k);
  for (long long i = 0; i <= n; ++i) {
    if ((k - i * a) >= 0 && (k - i * a) % b == 0 && 0 <= (k - i * a) / b &&
        (k - i * a) / b <= n)
      ans = add_mod(ans, mul_mod(ncr_mod(n, i), ncr_mod(n, (k - i * a) / b)));
  }
  printf("%lld\n", ans);
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 998244353;
LL f[300005], rf[300005];
LL inv(LL a, LL b) {
  LL res = 1;
  while (b) {
    if (b % 2)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
LL ncr(LL a, LL b) { return f[a] * rf[b] % mod * rf[a - b] % mod; }
int main() {
  LL n, a, b, k;
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);
  f[0] = 1;
  for (int i = 1; i <= n; i++)
    f[i] = (f[i - 1] * i) % mod;
  for (int i = 0; i <= n; i++)
    rf[i] = inv(f[i], mod - 2);
  if (k == 0) {
    printf("1\n");
    return 0;
  }
  LL ans = 0;
  for (int i = 0; i <= n; i++) {
    LL ca = i;
    LL cb = (k - ca * a);
    if (cb % b != 0)
      continue;
    cb /= b;
    if (max(ca, cb) > n)
      continue;
    ans = (ans + ncr(n, ca) * ncr(n, cb)) % mod;
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 998244353;
LL f[300005], rf[300005];
LL inv(LL a, LL b) {
  LL res = 1;
  while (b) {
    if (b % 2)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
LL ncr(LL a, LL b) { return f[a] * rf[b] % mod * rf[a - b] % mod; }
int main() {
  LL n, a, b, k;
  scanf("%lld%lld%lld%lld", &n, &a, &b, &k);
  f[0] = 1;
  for (int i = 1; i <= n; i++)
    f[i] = (f[i - 1] * i) % mod;
  for (int i = 0; i <= n; i++)
    rf[i] = inv(f[i], mod - 2);
  if (k == 0) {
    printf("1\n");
    return 0;
  }
  LL ans = 0;
  for (int i = 0; i <= n; i++) {
    LL ca = i;
    LL cb = (k - ca * a);
    if (cb < 0 || cb % b != 0)
      continue;
    cb /= b;
    if (max(ca, cb) > n)
      continue;
    ans = (ans + ncr(n, ca) * ncr(n, cb)) % mod;
  }
  printf("%lld\n", ans);
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
int const M = 3e5 + 10, mod = 998244353, inf = 1e9 + 10;
int fact[M], rev[M];
int pw(int x, int y) {
  if (y == 0)
    return 1;
  int tmp = pw(x, y / 2);
  if (y % 2 == 0)
    return (tmp * tmp) % mod;
  return ((tmp * tmp) % mod * x) % mod;
}
int C(int n, int r) {
  int tmp = rev[r] * rev[n - r];
  tmp %= mod;
  tmp *= fact[n];
  tmp %= mod;
  return tmp;
}
int32_t main() {
  int n, a, b, k;
  cin >> n >> a >> b >> k;
  fact[0] = 1;
  rev[0] = pw(1, mod - 2);
  for (int i = 1; i <= 3e5 + 2; i++)
    fact[i] = fact[i - 1] * i, fact[i] %= mod, rev[i] = pw(fact[i], mod - 2);
  int ans = 0;
  // cout<<C(5,0)<<endl;
  for (int i = 0; i <= n; i++) {
    int tmp = k - a * i;
    if (tmp % b != 0)
      continue;
    tmp /= b;
    if (tmp > n)
      continue;
    int hlp = C(n, i) * C(n, tmp);
    hlp %= mod;
    ans += hlp;
    ans %= mod;
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
int const M = 3e5 + 10, mod = 998244353, inf = 1e9 + 10;
int fact[M], rev[M];
int pw(int x, int y) {
  if (y == 0)
    return 1;
  int tmp = pw(x, y / 2);
  if (y % 2 == 0)
    return (tmp * tmp) % mod;
  return ((tmp * tmp) % mod * x) % mod;
}
int C(int n, int r) {
  int tmp = rev[r] * rev[n - r];
  tmp %= mod;
  tmp *= fact[n];
  tmp %= mod;
  return tmp;
}
int32_t main() {
  int n, a, b, k;
  cin >> n >> a >> b >> k;
  fact[0] = 1;
  rev[0] = pw(1, mod - 2);
  for (int i = 1; i <= 3e5 + 2; i++)
    fact[i] = fact[i - 1] * i, fact[i] %= mod, rev[i] = pw(fact[i], mod - 2);
  int ans = 0;
  // cout<<C(5,0)<<endl;
  for (int i = 0; i <= n; i++) {
    int tmp = k - a * i;
    if (tmp < 0)
      continue;
    if (tmp % b != 0)
      continue;
    tmp /= b;
    if (tmp > n)
      continue;
    int hlp = C(n, i) * C(n, tmp);
    hlp %= mod;
    ans += hlp;
    ans %= mod;
  }
  cout << ans;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

typedef long long ll;

constexpr int MAX_N = 500003;
constexpr ll mod = 998244353;
ll fact[MAX_N], inv[MAX_N];

ll powm(ll x, ll p) {
  if (p == 0)
    return 1;
  ll res = powm(x, p / 2);
  res *= res;
  res %= mod;
  if (p % 2LL == 1) {
    res *= x;
    res %= mod;
  }
  return res;
}

void init_fact() {
  fact[0] = inv[0] = 1;
  for (ll i = 1; i < MAX_N; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
    inv[i] = powm(fact[i], mod - 2);
  }
}

// aCb
ll C(ll a, ll b) {
  ll res = fact[a];
  res *= inv[b];
  res %= mod;
  res *= inv[a - b];
  return res % mod;
}

int main() {
  ll n, A, B, K;
  cin >> n >> A >> B >> K;
  init_fact();
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (K < i * A)
      continue;
    if ((K - i * A) % B != 0)
      continue;
    ll j = (K - i * A) / B;
    ans += C(n, i) * C(n, j) % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

typedef long long ll;

constexpr int MAX_N = 500003;
constexpr ll mod = 998244353;
ll fact[MAX_N], inv[MAX_N];

ll powm(ll x, ll p) {
  if (p == 0)
    return 1;
  ll res = powm(x, p / 2);
  res *= res;
  res %= mod;
  if (p % 2LL == 1) {
    res *= x;
    res %= mod;
  }
  return res;
}

void init_fact() {
  fact[0] = inv[0] = 1;
  for (ll i = 1; i < MAX_N; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
    inv[i] = powm(fact[i], mod - 2);
  }
}

// aCb
ll C(ll a, ll b) {
  ll res = fact[a];
  res *= inv[b];
  res %= mod;
  res *= inv[a - b];
  return res % mod;
}

int main() {
  ll n, A, B, K;
  cin >> n >> A >> B >> K;
  init_fact();
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    if (K < i * A)
      continue;
    if ((K - i * A) % B != 0)
      continue;
    ll j = (K - i * A) / B;
    if (j > n)
      continue;
    ans += C(n, i) * C(n, j) % mod;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p03332 (C++)

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

using namespace std;

typedef long long lli;
typedef double lld;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<double> vdl;
typedef vector<vector<lli>> mat;
typedef vector<vdl> mad;
typedef unordered_map<lli, unordered_map<lli, lli>> graph;
typedef complex<double> cmp;
typedef vector<cmp> vcl;

const lli mod = 998244353;

lli factorial(lli n) {
  static lli dp[100000];
  if (dp[n])
    return dp[n];
  if (n == 0)
    return dp[n] = 1;
  return dp[n] = (n * factorial(n - 1)) % mod;
}

lli pow(lli x, lli r, lli mod = 998244353) {
  lli ret = 1;
  for (; r != 0; r >>= 1) {
    if (r & 1 != 0)
      ret *= x, ret %= mod;
    x *= x, x %= mod;
  }
  return ret;
}

lli inverse(lli x, lli mod = 998244353) { return pow(x, mod - 2, mod); }

lli combination(lli n, lli m, lli mod = 998244353) {
  if (n < m)
    return 0;
  return (((factorial(n) * inverse(factorial(m))) % mod) *
          inverse(factorial(n - m))) %
         mod;
}

lli n, a, b, k;
lli ans = 0;

int main() {
  cin >> n >> a >> b >> k;
  for (lli i = 0; i <= n; i++) {
    if (k - a * i < 0)
      break;
    if ((k - a * i) % b == 0) {
      if ((k - a * i) / b <= n) {
        lli red = i;
        lli blue = (k - a * i) / b;
        ans += (combination(n, red) * combination(n, blue)) % mod;
        ans %= mod;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
typedef double lld;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<double> vdl;
typedef vector<vector<lli>> mat;
typedef vector<vdl> mad;
typedef unordered_map<lli, unordered_map<lli, lli>> graph;
typedef complex<double> cmp;
typedef vector<cmp> vcl;

const lli mod = 998244353;

lli factorial(lli n) {
  static lli dp[300001];
  if (dp[n])
    return dp[n];
  if (n == 0)
    return dp[n] = 1;
  return dp[n] = (n * factorial(n - 1)) % mod;
}

lli pow(lli x, lli r, lli mod = 998244353) {
  lli ret = 1;
  for (; r != 0; r >>= 1) {
    if (r & 1 != 0)
      ret *= x, ret %= mod;
    x *= x, x %= mod;
  }
  return ret;
}

lli inverse(lli x, lli mod = 998244353) { return pow(x, mod - 2, mod); }

lli combination(lli n, lli m, lli mod = 998244353) {
  if (n < m)
    return 0;
  return (((factorial(n) * inverse(factorial(m))) % mod) *
          inverse(factorial(n - m))) %
         mod;
}

lli n, a, b, k;
lli ans = 0;

int main() {
  cin >> n >> a >> b >> k;
  for (lli i = 0; i <= n; i++) {
    if (k - a * i < 0)
      break;
    if ((k - a * i) % b == 0) {
      if ((k - a * i) / b <= n) {
        lli red = i;
        lli blue = (k - a * i) / b;
        ans += (combination(n, red) * combination(n, blue)) % mod;
        ans %= mod;
      }
    }
  }
  cout << ans << endl;
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <random>
#include <vector>

using namespace std;

long long binom[300001];
long long MOD;

long long mult(long long a, long long b, long long p) { return (a * b) % p; }

long long inverse(long long a, long long p) {
  long long q;
  long long ans;
  long long d = a;
  q = p - 2;
  ans = 1;

  while (q > 0) {
    if (q % 2 == 1) {
      ans = mult(ans, d, p);
    }
    d = mult(d, d, p);
    q = q / 2;
  }
  return ans;
}

long long div(long long a, long long b, long long p) {
  return mult(a, inverse(b, p), p);
}

int main(void) {
  long long A, B, K, N;
  scanf("%lld %lld %lld %lld", &N, &A, &B, &K);

  MOD = 998244353;

  /*二項係数の計算*/
  binom[0] = 1;
  for (int i = 0; i < N; i++) {
    binom[i + 1] =
        div(mult(binom[i], N - (long long)i, MOD), (long long)i + 1, MOD);
  }

  long long ans = 0;

  for (int i = 0; i <= N; i++) {
    if ((K - i * A) % B == 0) {
      ans = (ans + mult(binom[i], binom[(K - i * A) / B], MOD)) % MOD;
    }
  }

  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <random>
#include <vector>

using namespace std;

long long binom[300001];
long long MOD;

long long mult(long long a, long long b, long long p) { return (a * b) % p; }

long long inverse(long long a, long long p) {
  long long q;
  long long ans;
  long long d = a;
  q = p - 2;
  ans = 1;

  while (q > 0) {
    if (q % 2 == 1) {
      ans = mult(ans, d, p);
    }
    d = mult(d, d, p);
    q = q / 2;
  }
  return ans;
}

long long div(long long a, long long b, long long p) {
  return mult(a, inverse(b, p), p);
}

int main(void) {
  long long A, B, K, N;
  scanf("%lld %lld %lld %lld", &N, &A, &B, &K);

  MOD = 998244353;

  /*二項係数の計算*/
  binom[0] = 1;
  for (int i = 0; i < N; i++) {
    binom[i + 1] =
        div(mult(binom[i], N - (long long)i, MOD), (long long)i + 1, MOD);
  }

  long long ans = 0;

  for (int i = 0; i <= N; i++) {
    if ((K - i * A) % B == 0 && 0 <= (K - i * A) / B && (K - i * A) / B <= N) {
      ans = (ans + mult(binom[i], binom[(K - i * A) / B], MOD)) % MOD;
    }
  }

  printf("%lld\n", ans);
}
Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long n, a, b, k, f[300001];
long long pow(long long a, long long b, long long MOD) {
  long long x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > MOD)
        x %= MOD;
    }
    y = (y * y);
    if (y > MOD)
      y %= MOD;
    b /= 2;
  }
  return x;
}

/* Modular Multiplicative Inverse
 Using Euler's Theorem
 a^(phi(m)) = 1 (mod m)
 a^(-1) = a^(m-2) (mod m) */
long long InverseEuler(long long n, long long MOD) {
  return pow(n, MOD - 2, MOD);
}

long long C(long long n, long long r, long long MOD) {

  return (f[n] *
          ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) %
         MOD;
}
int main() {
  cin >> n >> a >> b >> k;
  f[0] = 1;
  for (int i = 1; i <= 300000; i++) {
    f[i] = (f[i - 1] * i) % mod;
  }
  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    if ((k - (i * a)) < 0) {
      break;
    } else {
      if ((k - (i * a)) % b == 0) {
        ans += C(n, i, mod) * C(n, (k - i * a) / b, mod);
        ans %= mod;
      }
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long n, a, b, k, f[300001];
long long pow(long long a, long long b, long long MOD) {
  long long x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > MOD)
        x %= MOD;
    }
    y = (y * y);
    if (y > MOD)
      y %= MOD;
    b /= 2;
  }
  return x;
}

/* Modular Multiplicative Inverse
 Using Euler's Theorem
 a^(phi(m)) = 1 (mod m)
 a^(-1) = a^(m-2) (mod m) */
long long InverseEuler(long long n, long long MOD) {
  return pow(n, MOD - 2, MOD);
}

long long C(long long n, long long r, long long MOD) {

  return (f[n] *
          ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) %
         MOD;
}
int main() {
  cin >> n >> a >> b >> k;
  f[0] = 1;
  for (int i = 1; i <= 300000; i++) {
    f[i] = (f[i - 1] * i) % mod;
  }
  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    if ((k - (i * a)) < 0) {
      break;
    } else {
      if ((k - i * a) / b <= n && (k - (i * a)) % b == 0) {
        ans += C(n, i, mod) * C(n, (k - i * a) / b, mod);
        ans %= mod;
      }
    }
  }
  cout << ans;
}