Problem p03270 (C++)

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

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
// clang-format off
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":", ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream &o, tuple<T...> const &t){ o << "("; _ot<0>(o, t); o << ")"; return o; }
template<class T, class U> ostream & operator<<(ostream &o, pair<T, U> const &p) { o << "(" << p.first << ", " << p.second << ")"; return o; }
#ifdef DEBUG
#if !defined(DEBUG_OUT)
// #define DEBUG_OUT cerr
#endif
#define dump(...) [&](){auto __debug_tap=make_tuple(__VA_ARGS__);DEBUG_OUT<<"["<<__LINE__<< "] "<<#__VA_ARGS__<<" = "<<__debug_tap<<"\n";}()
template < class T > inline void dump2D(T &d, size_t sizey, size_t sizex) { for(size_t i = 0; i < sizey; i++) { DEBUG_OUT << "\t"; for(size_t j = 0; j < sizex; j++) DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t"); DEBUG_OUT << endl; } }
template < class T, class = typename iterator_traits< typename T::iterator >::value_type > ostream &operator<<(ostream &o, const T &a) { o << "{"; for(auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : ", ") << *ite; o << "}"; return o; }
#else
#define dump(...) (42)
#define dump2D(...) (42)
template < class T, class = typename iterator_traits< typename T::iterator >::value_type > ostream &operator<<(ostream &o, const T &a) { for(auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : " ") << *ite; return o; }
#endif
// clang-format on
// }}}

int k, n;

// constexpr int N = 1e5 + 10;
const int N = 2000;
const int mod = 998244353;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d;
  return b == 0 ? (x = 1, y = 0, a)
                : (d = extgcd(b, a % b, y, x), y -= a / b * x, d);
}
ll modinv(ll a, ll mod = 1e9 + 7) {
  ll x = 0, y = 0;
  extgcd(a, mod, x, y);
  return (x + mod) % mod;
}
ll modpow(ll a, ll b, ll mod = 1e9 + 7) {
  ll r = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
/// --- Modulo Factorial {{{ ///
template <int N, int mod = (int)1e9 + 7> struct Factorial {
  int arr[N + 1], inv[N + 1];
  ll operator[](int i) const { return arr[i]; }
  Factorial() : arr(), inv() {
    arr[0] = 1;
    for (int i = 1; i <= N; i++) {
      arr[i] = (ll)i * arr[i - 1] % mod;
    }
    inv[N] = modinv(arr[N], mod);
    for (int i = N - 1; i >= 0; i--) {
      inv[i] = (ll)(i + 1) * inv[i + 1] % mod;
    }
  }
  ll C(int n, int r) const {
    if (n < 0 || r < 0 || n < r)
      return 0;
    assert(n <= N);
    return (ll)arr[n] * inv[r] % mod * inv[n - r] % mod;
  }
  ll H(int n, int r) const { return C(n + r - 1, r); }
};
/// }}}--- ///
Factorial<N, mod> fact;

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> k >> n;
  for (int i = 2; i <= 2 * k; i++) {
    int y = 0;
    if (i % 2 == 0)
      y = 1;
    int x = (i - 1) / 2;
    int m = max(1, i - k);
    x = max(0, min(x, x - m + 1));
    ll ans = 0;
    dump(x, y, i);
    for (int j = 0; j <= x; j++) {
      if (k - x - j - y < 0)
        continue;
      if (n - y - (x - j) >= 0)
        ans += modpow(2, x - j, mod) * fact.C(x, j) % mod *
               fact.H(k - x - j - y, n - y - (x - j)) % mod;
      if (y) {
        if (n - (x - j) >= 0)
          ans += modpow(2, x - j, mod) * fact.C(x, j) % mod *
                 fact.H(k - x - j - y, n - (x - j)) % mod;
      }
    }
    cout << (ans % mod + mod) % mod << endl;
  }
  return 0;
}


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

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
// clang-format off
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":", ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream &o, tuple<T...> const &t){ o << "("; _ot<0>(o, t); o << ")"; return o; }
template<class T, class U> ostream & operator<<(ostream &o, pair<T, U> const &p) { o << "(" << p.first << ", " << p.second << ")"; return o; }
#ifdef DEBUG
#if !defined(DEBUG_OUT)
// #define DEBUG_OUT cerr
#endif
#define dump(...) [&](){auto __debug_tap=make_tuple(__VA_ARGS__);DEBUG_OUT<<"["<<__LINE__<< "] "<<#__VA_ARGS__<<" = "<<__debug_tap<<"\n";}()
template < class T > inline void dump2D(T &d, size_t sizey, size_t sizex) { for(size_t i = 0; i < sizey; i++) { DEBUG_OUT << "\t"; for(size_t j = 0; j < sizex; j++) DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t"); DEBUG_OUT << endl; } }
template < class T, class = typename iterator_traits< typename T::iterator >::value_type > ostream &operator<<(ostream &o, const T &a) { o << "{"; for(auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : ", ") << *ite; o << "}"; return o; }
#else
#define dump(...) (42)
#define dump2D(...) (42)
template < class T, class = typename iterator_traits< typename T::iterator >::value_type > ostream &operator<<(ostream &o, const T &a) { for(auto ite = a.begin(); ite != a.end(); ++ite) o << (ite == a.begin() ? "" : " ") << *ite; return o; }
#endif
// clang-format on
// }}}

int k, n;

// constexpr int N = 1e5 + 10;
const int N = 2000;
const int mod = 998244353;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d;
  return b == 0 ? (x = 1, y = 0, a)
                : (d = extgcd(b, a % b, y, x), y -= a / b * x, d);
}
ll modinv(ll a, ll mod = 1e9 + 7) {
  ll x = 0, y = 0;
  extgcd(a, mod, x, y);
  return (x + mod) % mod;
}
ll modpow(ll a, ll b, ll mod = 1e9 + 7) {
  ll r = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
/// --- Modulo Factorial {{{ ///
template <int N, int mod = (int)1e9 + 7> struct Factorial {
  int arr[N + 1], inv[N + 1];
  ll operator[](int i) const { return arr[i]; }
  Factorial() : arr(), inv() {
    arr[0] = 1;
    for (int i = 1; i <= N; i++) {
      arr[i] = (ll)i * arr[i - 1] % mod;
    }
    inv[N] = modinv(arr[N], mod);
    for (int i = N - 1; i >= 0; i--) {
      inv[i] = (ll)(i + 1) * inv[i + 1] % mod;
    }
  }
  ll C(int n, int r) const {
    if (n < 0 || r < 0 || n < r)
      return 0;
    assert(n <= N);
    return (ll)arr[n] * inv[r] % mod * inv[n - r] % mod;
  }
  ll H(int n, int r) const { return C(n + r - 1, r); }
};
/// }}}--- ///
Factorial<N * 4, mod> fact;

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> k >> n;
  for (int i = 2; i <= 2 * k; i++) {
    int y = 0;
    if (i % 2 == 0)
      y = 1;
    int x = (i - 1) / 2;
    int m = max(1, i - k);
    x = max(0, min(x, x - m + 1));
    ll ans = 0;
    dump(x, y, i);
    for (int j = 0; j <= x; j++) {
      if (k - x - j - y < 0)
        continue;
      if (n - y - (x - j) >= 0)
        ans += modpow(2, x - j, mod) * fact.C(x, j) % mod *
               fact.H(k - x - j - y, n - y - (x - j)) % mod;
      if (y) {
        if (n - (x - j) >= 0)
          ans += modpow(2, x - j, mod) * fact.C(x, j) % mod *
                 fact.H(k - x - j - y, n - (x - j)) % mod;
      }
    }
    cout << (ans % mod + mod) % mod << endl;
  }
  return 0;
}