Problem p03332 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
const int mod = 998244353;
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
vector<int> f = {1}, fi = {1};
int fact(int x) {
  while ((int)f.size() <= x)
    f.push_back((f.back() * f.size()) % mod);
  return f[x];
}
int ifact(int x) { return modpow(fact(x), mod - 2, mod); }
int C(int x, int y) {
  if (x < y)
    return 0;
  return fact(x) * ifact(y) % mod * ifact(x - y) % mod;
}
signed main() {
  int n, a, b, k;
  cin >> n >> a >> b >> k;
  // ax + by = k
  vector<P> kouho;
  bool found = false;
  for (int x = 0; x <= n; x++) {
    int tmp = k - a * x;
    if (tmp % b == 0) {
      int y = tmp / b;
      if (y >= 0 && y <= n) {
        // trace(x,y);
        kouho.emplace_back(x, y);
        found = true;
      }
    }
  }
  // for(auto p:kouho)trace(p);
  if (kouho.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 0;
  rep(i, (int)kouho.size()) {
    int x = kouho[i].first, y = kouho[i].second;
    for (int g = 0; g <= min(x, y); g++) {
      int ret = 1;
      int r = x - g;
      int b = y - g;
      int c = n - g - r - b;

      if (c < 0)
        continue;
      // trace(x,y,g,r,b,c,ret);
      ret = (ret * fact(n)) % mod;
      ret = (ret * ifact(r)) % mod;
      ret = (ret * ifact(g)) % mod;
      ret = (ret * ifact(b)) % mod;
      ret = (ret * ifact(c)) % mod;
      ans = (ans + ret) % mod;
    }
  }
  cout << ans % mod << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
const int mod = 998244353;
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
vector<int> f = {1}, fi = {1};
int fact(int x) {
  while ((int)f.size() <= x)
    f.push_back((f.back() * f.size()) % mod);
  return f[x];
}
int ifact(int x) { return modpow(fact(x), mod - 2, mod); }
int C(int x, int y) {
  if (x < y)
    return 0;
  return fact(x) * ifact(y) % mod * ifact(x - y) % mod;
}
signed main() {
  int n, a, b, k;
  cin >> n >> a >> b >> k;
  // ax + by = k
  vector<P> kouho;
  bool found = false;
  for (int x = 0; x <= n; x++) {
    int tmp = k - a * x;
    if (tmp % b == 0) {
      int y = tmp / b;
      if (y >= 0 && y <= n) {
        // trace(x,y);
        kouho.emplace_back(x, y);
        found = true;
      }
    }
  }
  // for(auto p:kouho)trace(p);
  if (kouho.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 0;
  rep(i, (int)kouho.size()) {
    int x = kouho[i].first, y = kouho[i].second;
    int ret = 1;
    ret = (ret * C(n, x)) % mod;
    ret = (ret * C(n, y)) % mod;
    ans = (ans + ret) % mod;
  }
  cout << ans % mod << endl;

  return 0;
}