Problem p03270 (C++)

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

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int maxn = 2009;
const int mod = 998244353;
int c[maxn + 1][maxn + 1];

void pre() {
  c[0][0] = 1;
  repA(n, 1, maxn) {
    c[n][0] = c[n][n] = 1;
    repA(k, 1, n - 1) c[n][k] = (c[n - 1][k - 1] + c[n - 1][k]) % mod;
  }
}

void solve() {}

int f(int n, int k) {
  if (n < 0 || k < 0)
    return 0;
  return c[n][k];
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();

  vector<ll> p(2009, 1);
  repA(i, 1, 2008) p[i] = p[i - 1] * 2 % mod;

  int k, n;
  cin >> k >> n;

  repA(i, 2, 2 * k) {
    ll ans = 0, h, m;

    if (i > k)
      h = (2 * k + 1 - i) / 2, m = i - k - 1 + h;
    else
      h = (i - 1) / 2, m = k - i + 1 + h;

    repA(j, 0, h) ans +=
        f(n + m - h - 1, m - h + j - 1) * p[j] % mod * f(h, h - j) % mod,
        ans %= mod;

    n--;
    if (i % 2 == 0)
      repA(j, 0, h) ans +=
          f(n + m - h - 1, m - h + j - 1) * p[j] % mod * f(h, h - j) % mod,
          ans %= mod;
    n++;

    cout << ans << endl;
  }

  return 0;
}


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

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int maxn = 4009;
const int mod = 998244353;
int c[maxn + 1][maxn + 1];

void pre() {
  c[0][0] = 1;
  repA(n, 1, maxn) {
    c[n][0] = c[n][n] = 1;
    repA(k, 1, n - 1) c[n][k] = (c[n - 1][k - 1] + c[n - 1][k]) % mod;
  }
}

void solve() {}

int f(int n, int k) {
  if (n < 0 || k < 0)
    return 0;
  return c[n][k];
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();

  vector<ll> p(2009, 1);
  repA(i, 1, 2008) p[i] = p[i - 1] * 2 % mod;

  int k, n;
  cin >> k >> n;

  repA(i, 2, 2 * k) {
    ll ans = 0, h, m;

    if (i > k)
      h = (2 * k + 1 - i) / 2, m = i - k - 1 + h;
    else
      h = (i - 1) / 2, m = k - i + 1 + h;

    repA(j, 0, h) ans +=
        f(n + m - h - 1, m - h + j - 1) * p[j] % mod * f(h, h - j) % mod,
        ans %= mod;

    n--;
    if (i % 2 == 0)
      repA(j, 0, h) ans +=
          f(n + m - h - 1, m - h + j - 1) * p[j] % mod * f(h, h - j) % mod,
          ans %= mod;
    n++;

    cout << ans << endl;
  }

  return 0;
}