Problem p03270 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = 998244353;

long long k, n, dat[5000], inv[5000];
vector<long long> ans;

long long quickpow(long long b, long long e) {
  return e ? ((e & 1 ? b : 1) * quickpow((b * b) % MOD, e >> 1)) % MOD : 1;
}

long long combi(int a, int b) {
  if (a < 0 || b < 0)
    return 0;
  long long retval = dat[a];
  retval = retval * inv[a - b];
  retval %= MOD;
  retval = retval * inv[b];
  retval %= MOD;
  return retval;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> k >> n;
  dat[0] = 1;
  for (int i = 1; i < n + k; i++) {
    dat[i] = dat[i - 1] * i;
    dat[i] %= MOD;
  }
  for (int i = 0; i < n + k; i++) {
    inv[i] = quickpow(dat[i], MOD - 2);
  }
  for (int i = 2; i <= k + k; i++) {
    int tmp = 0;
    for (int j = 1; j <= k; j++) {
      if (i - j <= j)
        break;
      if (i - j <= k)
        tmp++;
    }
    long long cur, tt;
    if (i % 2 == 0) {
      cur = 0;
      for (int i = 0; i <= tmp; i++) {
        tt = combi(n + k - 2 - tmp - (tmp - i) - i, k - 2 - tmp - (tmp - i));
        tt = tt * combi(tmp, i);
        tt %= MOD;
        cur += tt * quickpow(2, i);
        cur %= MOD;
      }
      for (int i = 0; i <= tmp; i++) {
        tt = combi(n + k - 3 - tmp - (tmp - i) - i, k - 2 - tmp - (tmp - i));
        tt = tt * combi(tmp, i);
        tt %= MOD;
        cur += tt * quickpow(2, i);
        cur %= MOD;
      }
      ans.pb(cur);
    } else {
      cur = 0;
      for (int i = 0; i <= tmp; i++) {
        tt = combi(n + k - 1 - tmp - (tmp - i) - i, k - 1 - tmp - (tmp - i));
        tt = tt * combi(tmp, i);
        tt %= MOD;
        cur += tt * quickpow(2, i);
        cur %= MOD;
      }
      ans.pb(cur);
    }
  }
  for (int i = 0; i < (int)ans.size(); i++) {
    cout << ans[i] << E;
  }
  // system("pause");
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = 998244353;

long long k, n, dat[5000], inv[5000];
vector<long long> ans;

long long quickpow(long long b, long long e) {
  return e ? ((e & 1 ? b : 1) * quickpow((b * b) % MOD, e >> 1)) % MOD : 1;
}

long long combi(int a, int b) {
  if (a < 0 || b < 0 || a < b)
    return 0;
  long long retval = dat[a];
  retval = retval * inv[a - b];
  retval %= MOD;
  retval = retval * inv[b];
  retval %= MOD;
  return retval;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> k >> n;
  dat[0] = 1;
  for (int i = 1; i < n + k; i++) {
    dat[i] = dat[i - 1] * i;
    dat[i] %= MOD;
  }
  for (int i = 0; i < n + k; i++) {
    inv[i] = quickpow(dat[i], MOD - 2);
  }
  for (int i = 2; i <= k + k; i++) {
    int tmp = 0;
    for (int j = 1; j <= k; j++) {
      if (i - j <= j)
        break;
      if (i - j <= k)
        tmp++;
    }
    long long cur, tt;
    if (i % 2 == 0) {
      cur = 0;
      for (int i = 0; i <= tmp; i++) {
        tt = combi(n + k - 2 - tmp - (tmp - i) - i, k - 2 - tmp - (tmp - i));
        tt = tt * combi(tmp, i);
        tt %= MOD;
        cur += tt * quickpow(2, i);
        cur %= MOD;
      }
      for (int i = 0; i <= tmp; i++) {
        tt = combi(n + k - 3 - tmp - (tmp - i) - i, k - 2 - tmp - (tmp - i));
        tt = tt * combi(tmp, i);
        tt %= MOD;
        cur += tt * quickpow(2, i);
        cur %= MOD;
      }
      ans.pb(cur);
    } else {
      cur = 0;
      for (int i = 0; i <= tmp; i++) {
        tt = combi(n + k - 1 - tmp - (tmp - i) - i, k - 1 - tmp - (tmp - i));
        tt = tt * combi(tmp, i);
        tt %= MOD;
        cur += tt * quickpow(2, i);
        cur %= MOD;
      }
      ans.pb(cur);
    }
  }
  for (int i = 0; i < (int)ans.size(); i++) {
    cout << ans[i] << E;
  }
  // system("pause");
  return 0;
}