Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#define _FORTIFY_SOURCE 0
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define int long long
#define double long double
#define Matrix vector<vector<int>>
#define bublic public
#define kektor vector
#define x1 x1228
#define y1 y1228
#define pii pair<int, int>
#define pp pair
#define left left1
#define right right1
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rand() (rand() << 15 | rand())

using namespace std;

typedef long long ll;

const int maxn = 4e5, mod = 998244353, inf = 1e18;
const double eps = 1e-9, pi = acos(-1);
int n, a, b, k, ans = 0, power[maxn];

int add(int p1, int p2) { return (p1 + p2) % mod; }

int mul(int p1, int p2) { return (p1 * p2) % mod; }

int fp(int a, int b) {
  if (b == 0)
    return 1;
  int tmp = fp(a, b / 2);
  tmp = mul(tmp, tmp);
  if (b % 2)
    tmp = mul(a, tmp);
  return tmp;
}

int inv(int a) { return fp(a, mod - 2); }

int C_n_k(int n1, int k1) {
  return mul(power[n1], mul(inv(power[n1 - k1]), inv(power[k1])));
}

void solve() {
  cin >> n >> a >> b >> k;
  power[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    power[i] = mul(power[i - 1], i);
  }
  for (int A = 0; A <= n; ++A) {
    int B = k - A * a;
    if (B < 0 || B % b != 0)
      continue;
    B /= b;
    ans = add(ans, mul(C_n_k(n, A), C_n_k(n, B)));
  }
  cout << ans;
}

signed main() {
//  freopen("bricks.in", "r", stdin);
//  freopen("bricks.out", "w", stdout);
#ifdef offline_judge
  freopen("TASK.in", "r", stdin);
  freopen("TASK.out", "w", stdout);
#endif
  srand(228);
  cout.precision(10);
  cout << fixed;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}

Fixed submission:
#define _FORTIFY_SOURCE 0
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define int long long
#define double long double
#define Matrix vector<vector<int>>
#define bublic public
#define kektor vector
#define x1 x1228
#define y1 y1228
#define pii pair<int, int>
#define pp pair
#define left left1
#define right right1
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rand() (rand() << 15 | rand())

using namespace std;

typedef long long ll;

const int maxn = 4e5, mod = 998244353, inf = 1e18;
const double eps = 1e-9, pi = acos(-1);
int n, a, b, k, ans = 0, power[maxn];

int add(int p1, int p2) { return (p1 + p2) % mod; }

int mul(int p1, int p2) { return (p1 * p2) % mod; }

int fp(int a, int b) {
  if (b == 0)
    return 1;
  int tmp = fp(a, b / 2);
  tmp = mul(tmp, tmp);
  if (b % 2)
    tmp = mul(a, tmp);
  return tmp;
}

int inv(int a) { return fp(a, mod - 2); }

int C_n_k(int n1, int k1) {
  return mul(power[n1], mul(inv(power[n1 - k1]), inv(power[k1])));
}

void solve() {
  cin >> n >> a >> b >> k;
  power[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    power[i] = mul(power[i - 1], i);
  }
  for (int A = 0; A <= n; ++A) {
    int B = k - A * a;
    if (B < 0 || B % b != 0)
      continue;
    B /= b;
    if (B > n)
      continue;
    ans = add(ans, mul(C_n_k(n, A), C_n_k(n, B)));
  }
  cout << ans;
}

signed main() {
//  freopen("bricks.in", "r", stdin);
//  freopen("bricks.out", "w", stdout);
#ifdef offline_judge
  freopen("TASK.in", "r", stdin);
  freopen("TASK.out", "w", stdout);
#endif
  srand(228);
  cout.precision(10);
  cout << fixed;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}