Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef int sign;
typedef long long ll;
#define For(i, a, b) for (register sign i = (sign)(a); i <= (sign)(b); ++i)
#define Fordown(i, a, b) for (register sign i = (sign)(a); i >= (sign)(b); --i)
const int N = 3e5 + 5;
template <typename T> bool cmax(T &a, T b) { return (a < b) ? a = b, 1 : 0; }
template <typename T> bool cmin(T &a, T b) { return (a > b) ? a = b, 1 : 0; }
template <typename T> T read() {
  T ans = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    f = -1, ch = getchar();
  while (isdigit(ch))
    ans = (ans << 3) + (ans << 1) + (ch - '0'), ch = getchar();
  return ans * f;
}
template <typename T> void write(T x, char y) {
  if (x == 0) {
    putchar('0'), putchar(y);
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  static char wr[20];
  int top = 0;
  for (; x; x /= 10)
    wr[++top] = x % 10 + '0';
  while (top)
    putchar(wr[top--]);
  putchar(y);
}
void file() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
}
int n;
ll A, B, K;
void input() {
  n = read<int>();
  A = read<ll>(), B = read<ll>(), K = read<ll>();
}
const int mo = 998244353;
int mc[N], inv[N];
ll power(ll x, int y) {
  ll res = 1;
  for (; y; x = x * x % mo, y >>= 1)
    if (y & 1)
      res = res * x % mo;
  return res;
}
int C(int x, int y) { return (ll)mc[x] * inv[y] % mo * inv[x - y] % mo; }
int sum[N], ans;
int cal(int i) {
  ll res = K - A * i;
  if (res < 0)
    return 0;
  else {
    if (res % B)
      return 0;
    return C(n, res / B);
  }
}
void work() {
  mc[0] = inv[0] = 1;
  For(i, 1, n) mc[i] = (ll)mc[i - 1] * i % mo;
  inv[n] = power(mc[n], mo - 2);
  Fordown(i, n - 1, 1) inv[i] = (ll)inv[i + 1] * (i + 1) % mo;
  Fordown(i, n, 0) sum[i] = (sum[i + 1] + C(n, i)) % mo;
  For(i, 0, n) {
    ans = (ans + 1ll * C(n, i) * cal(i) % mo) % mo;
    // cout<<i<<' '<<cal(i)<<' '<<C(n,i)<<' '<<sum[cal(i)]<<endl;
  }
  write(ans, '\n');
}
int main() {
  //	file();
  input();
  work();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef int sign;
typedef long long ll;
#define For(i, a, b) for (register sign i = (sign)(a); i <= (sign)(b); ++i)
#define Fordown(i, a, b) for (register sign i = (sign)(a); i >= (sign)(b); --i)
const int N = 3e5 + 5;
template <typename T> bool cmax(T &a, T b) { return (a < b) ? a = b, 1 : 0; }
template <typename T> bool cmin(T &a, T b) { return (a > b) ? a = b, 1 : 0; }
template <typename T> T read() {
  T ans = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    f = -1, ch = getchar();
  while (isdigit(ch))
    ans = (ans << 3) + (ans << 1) + (ch - '0'), ch = getchar();
  return ans * f;
}
template <typename T> void write(T x, char y) {
  if (x == 0) {
    putchar('0'), putchar(y);
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  static char wr[20];
  int top = 0;
  for (; x; x /= 10)
    wr[++top] = x % 10 + '0';
  while (top)
    putchar(wr[top--]);
  putchar(y);
}
void file() {
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
}
int n;
ll A, B, K;
void input() {
  n = read<int>();
  A = read<ll>(), B = read<ll>(), K = read<ll>();
}
const int mo = 998244353;
int mc[N], inv[N];
ll power(ll x, int y) {
  ll res = 1;
  for (; y; x = x * x % mo, y >>= 1)
    if (y & 1)
      res = res * x % mo;
  return res;
}
int C(int x, int y) { return (ll)mc[x] * inv[y] % mo * inv[x - y] % mo; }
int sum[N], ans;
int cal(int i) {
  ll res = K - A * i;
  if (res < 0)
    return 0;
  else {
    if (res % B)
      return 0;
    if (res / B > n)
      return 0;
    return C(n, res / B);
  }
}
void work() {
  mc[0] = inv[0] = 1;
  For(i, 1, n) mc[i] = (ll)mc[i - 1] * i % mo;
  inv[n] = power(mc[n], mo - 2);
  Fordown(i, n - 1, 1) inv[i] = (ll)inv[i + 1] * (i + 1) % mo;
  Fordown(i, n, 0) sum[i] = (sum[i + 1] + C(n, i)) % mo;
  For(i, 0, n) {
    ans = (ans + 1ll * C(n, i) * cal(i) % mo) % mo;
    // cout<<i<<' '<<cal(i)<<' '<<C(n,i)<<' '<<sum[cal(i)]<<endl;
  }
  write(ans, '\n');
}
int main() {
  //	file();
  input();
  work();
  return 0;
}