Problem p03270 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long int ll;
static const int MAX_K = 2000;
static const int MAX_N = 2000;
static const ll MOD = 998244353;

int K, N;
ll nCr[MAX_K + MAX_N + 1][MAX_N + 1];

ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = mod_pow((x * x) % MOD, n / 2);
  if (n % 2 == 1)
    res = (res * x) % MOD;
  return res;
}

void init() {
  for (int i = 0; i <= N + K; i++) {
    for (int j = 0; j <= K; j++) {
      if (i == 0 && j == 0)
        nCr[i][j] = 1;
      else if (j == 0)
        nCr[i][j] = nCr[i - 1][j];
      else
        nCr[i][j] = nCr[i - 1][j - 1] + nCr[i - 1][j];
      nCr[i][j] %= MOD;
    }
  }
  return;
}

ll binomial(int n, int r) {
  if (n < 0 || r < 0 || r > n)
    return 0;
  else
    return nCr[n][r];
}

int main() {
  scanf("%d %d", &K, &N);
  init();
  for (int i = 2; i <= 2 * K; i++) {
    if (i % 2 == 1) {
      ll sum = 0;
      int num = (i > K + 1 ? (2 * K - i + 1) / 2 : (i - 1) / 2);
      for (int j = 0; j <= num; j++) {
        ll cnt = mod_pow(2, num - j);
        cnt *= binomial(num, num - j);
        cnt %= MOD;
        cnt *= binomial(N - (num - j) + (K - 2 * j - (num - j) - 1),
                        K - 2 * j - (num - j) - 1);
        cnt %= MOD;
        sum += cnt;
        sum %= MOD;
      }
      printf("%lld\n", sum);
    } else {
      ll sum = 0;
      int num = (i > K + 1 ? (2 * K - i + 1) / 2 : (i - 1) / 2);
      for (int j = 0; j <= num; j++) {
        ll cnt = mod_pow(2, num - j);
        cnt *= binomial(num, num - j);
        cnt %= MOD;
        cnt *= binomial(N - (num - j) + (K - 2 * j - (num - j) - 2),
                        K - 2 * j - (num - j) - 2);
        cnt %= MOD;
        sum += cnt;
        sum %= MOD;
        cnt = mod_pow(2, num - j);
        cnt *= binomial(num, num - j);
        cnt %= MOD;
        cnt *= binomial(N - 1 - (num - j) + (K - 2 * j - (num - j) - 2),
                        K - 2 * j - (num - j) - 2);
        cnt %= MOD;
        sum += cnt;
        sum %= MOD;
      }
      printf("%lld\n", sum);
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long int ll;
static const int MAX_K = 2000;
static const int MAX_N = 2000;
static const ll MOD = 998244353;

int K, N;
ll nCr[MAX_K + MAX_N + 1][MAX_N + 1];

ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = mod_pow((x * x) % MOD, n / 2);
  if (n % 2 == 1)
    res = (res * x) % MOD;
  return res;
}

void init() {
  for (int i = 0; i <= N + K; i++) {
    for (int j = 0; j <= K; j++) {
      if (i == 0 && j == 0)
        nCr[i][j] = 1;
      else if (i == 0)
        nCr[i][j] = 0;
      else if (j == 0)
        nCr[i][j] = nCr[i - 1][j];
      else
        nCr[i][j] = nCr[i - 1][j - 1] + nCr[i - 1][j];
      nCr[i][j] %= MOD;
    }
  }
  return;
}

ll binomial(int n, int r) {
  if (n < 0 || r < 0 || r > n)
    return 0;
  else
    return nCr[n][r];
}

int main() {
  scanf("%d %d", &K, &N);
  init();
  for (int i = 2; i <= 2 * K; i++) {
    if (i % 2 == 1) {
      ll sum = 0;
      int num = (i > K + 1 ? (2 * K - i + 1) / 2 : (i - 1) / 2);
      for (int j = 0; j <= num; j++) {
        ll cnt = mod_pow(2, num - j);
        cnt *= binomial(num, num - j);
        cnt %= MOD;
        cnt *= binomial(N - (num - j) + (K - 2 * j - (num - j) - 1),
                        K - 2 * j - (num - j) - 1);
        cnt %= MOD;
        sum += cnt;
        sum %= MOD;
      }
      printf("%lld\n", sum);
    } else {
      ll sum = 0;
      int num = (i > K + 1 ? (2 * K - i + 1) / 2 : (i - 1) / 2);
      for (int j = 0; j <= num; j++) {
        ll cnt = mod_pow(2, num - j);
        cnt *= binomial(num, num - j);
        cnt %= MOD;
        cnt *= binomial(N - (num - j) + (K - 2 * j - (num - j) - 2),
                        K - 2 * j - (num - j) - 2);
        cnt %= MOD;
        sum += cnt;
        sum %= MOD;
        cnt = mod_pow(2, num - j);
        cnt *= binomial(num, num - j);
        cnt %= MOD;
        cnt *= binomial(N - 1 - (num - j) + (K - 2 * j - (num - j) - 2),
                        K - 2 * j - (num - j) - 2);
        cnt %= MOD;
        sum += cnt;
        sum %= MOD;
      }
      printf("%lld\n", sum);
    }
  }
  return 0;
}