Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <deque>         // deque
#include <functional>    // std::function<void(int)>
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

constexpr int MOD = 998244353;
struct mint {
  int v;
  mint() : v(0) {}
  mint(int v) : v((v + MOD) % MOD) {}
  mint operator-() const { return mint(0) - *this; }
  mint &operator+=(const mint &a) {
    if ((v += a.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((v += MOD - a.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (v *= a.v) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a) {
    (*this) *= a.inv();
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return v < a.v; }
  bool operator==(const mint &a) const { return v == a.v; }
  mint pow(int k) const {
    mint r(1), t(v);
    while (k) {
      if (k & 1)
        r *= t;
      t *= t;
      k >>= 1;
    }
    return r;
  }
  mint inv() const { return pow(MOD - 2); }
  static mint comb(int n, int k) {
    if (n - k < k)
      k = n - k;
    mint num(1), dom(1);
    for (int i = 0; i < k; i++) {
      num *= n - i;
      dom *= i + 1;
    }
    return num / dom;
  }
};
std::istream &operator>>(std::istream &i, mint &a) {
  int t;
  i >> t;
  a = mint(t);
  return i;
}
std::ostream &operator<<(std::ostream &o, const mint &a) {
  o << a.v;
  return o;
}

signed main() {
  int n, a, b, k;
  std::cin >> n >> a >> b >> k;

  std::vector<mint> combn(n + 1);
  mint comb = 1;
  rep(i, n + 1) {
    combn[i] = comb;
    if (i == n)
      break;
    comb *= mint(n - i);
    comb /= mint(i + 1);
  }

  mint count = 0;
  for (int sa = 0; sa <= n; sa++) {
    int x = k - sa * a;
    if (x >= 0 && x % b == 0) {
      int sb = x / b;
      count += combn[sa] * combn[sb];
    }
  }

  print(count);

  return 0;
}


Fixed submission:
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <deque>         // deque
#include <functional>    // std::function<void(int)>
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

constexpr int MOD = 998244353;
struct mint {
  int v;
  mint() : v(0) {}
  mint(int v) : v((v + MOD) % MOD) {}
  mint operator-() const { return mint(0) - *this; }
  mint &operator+=(const mint &a) {
    if ((v += a.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((v += MOD - a.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (v *= a.v) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a) {
    (*this) *= a.inv();
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return v < a.v; }
  bool operator==(const mint &a) const { return v == a.v; }
  mint pow(int k) const {
    mint r(1), t(v);
    while (k) {
      if (k & 1)
        r *= t;
      t *= t;
      k >>= 1;
    }
    return r;
  }
  mint inv() const { return pow(MOD - 2); }
  static mint comb(int n, int k) {
    if (n - k < k)
      k = n - k;
    mint num(1), dom(1);
    for (int i = 0; i < k; i++) {
      num *= n - i;
      dom *= i + 1;
    }
    return num / dom;
  }
};
std::istream &operator>>(std::istream &i, mint &a) {
  int t;
  i >> t;
  a = mint(t);
  return i;
}
std::ostream &operator<<(std::ostream &o, const mint &a) {
  o << a.v;
  return o;
}

signed main() {
  int n, a, b, k;
  std::cin >> n >> a >> b >> k;

  std::vector<mint> combn(n + 1);
  mint comb = 1;
  rep(i, n + 1) {
    combn[i] = comb;
    if (i == n)
      break;
    comb *= mint(n - i);
    comb /= mint(i + 1);
  }

  mint count = 0;
  for (int sa = 0; sa <= n; sa++) {
    int x = k - sa * a;
    if (x >= 0 && x % b == 0) {
      int sb = x / b;
      if (sb <= n)
        count += combn[sa] * combn[sb];
    }
  }

  print(count);

  return 0;
}