Problem p03066 (C++)

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

const int64_t MOD = 998244353;
void add(int64_t &a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t &a, int64_t b) { a = (a * b) % MOD; }

vector<int64_t> fact, seq_inv, fact_inv;

void create_fact_mod(int num) {
  fact[0] = 1;
  fact[1] = 1;
  for (int i = 2; i <= num; i++) {
    fact[i] = fact[i - 1] * i % MOD;
  }
}

void create_seq_inv_mod(int num) {
  seq_inv[0] = 1;
  seq_inv[1] = 1;
  for (int i = 2; i <= num; i++) {
    seq_inv[i] = (MOD - MOD / i) * seq_inv[MOD % i] % MOD;
  }
}

void create_fact_inv_mod(int num) {
  fact_inv[0] = 1;
  fact_inv[1] = 1;
  for (int i = 2; i <= num; i++) {
    fact_inv[i] = fact_inv[i - 1] * seq_inv[i] % MOD;
  }
}

void create_mod_tables(int num) {
  fact.resize(num + 1);
  seq_inv.resize(num + 1);
  fact_inv.resize(num + 1);
  create_fact_mod(num);
  create_seq_inv_mod(num);
  create_fact_inv_mod(num);
}

int64_t comb_mod(int n, int k) {
  return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD;
}

int64_t perm_mod(int n, int k) { return fact[n] * fact_inv[n - k] % MOD; }

int main() {
  int N, X;
  cin >> N >> X;
  create_mod_tables(6010);

  static int64_t dp[3001][3010], sub[3001][3010];
  dp[0][0] = 1;
  sub[0][0] = 1;
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= 2 * N; j++) {
      for (int k = 0; k <= 2; k++)
        add(dp[i + 1][j + k], dp[i][j]);
      for (int k = 1; k <= 2; k++)
        add(sub[i + 1][j + k], sub[i][j]);
    }
  int64_t ans = 0;
  for (int j = 0; j < X; j++)
    add(ans, dp[N][j]);

  for (int t = X + 1; t <= 2 * N; t += 2) {
    int s = t + 1 - X;
    if (X % 2 == 0 && X <= s)
      break;
    if (s <= X - 1) {
      for (int d = 0; d <= N; d++) {
        int64_t result = 1;
        mul(result, sub[d][X - 1 - s]);
        int stp = s / 2 + d + (t - (X - 1)) / 2;
        if (stp > N)
          continue;
        mul(result, comb_mod(N, stp));
        add(ans, result);
      }
    } else {
      add(ans, comb_mod(N, t / 2));
    }
  }
  cout << ans << endl;
  return 0;
}


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

const int64_t MOD = 998244353;
void add(int64_t &a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t &a, int64_t b) { a = (a * b) % MOD; }

vector<int64_t> fact, seq_inv, fact_inv;

void create_fact_mod(int num) {
  fact[0] = 1;
  fact[1] = 1;
  for (int i = 2; i <= num; i++) {
    fact[i] = fact[i - 1] * i % MOD;
  }
}

void create_seq_inv_mod(int num) {
  seq_inv[0] = 1;
  seq_inv[1] = 1;
  for (int i = 2; i <= num; i++) {
    seq_inv[i] = (MOD - MOD / i) * seq_inv[MOD % i] % MOD;
  }
}

void create_fact_inv_mod(int num) {
  fact_inv[0] = 1;
  fact_inv[1] = 1;
  for (int i = 2; i <= num; i++) {
    fact_inv[i] = fact_inv[i - 1] * seq_inv[i] % MOD;
  }
}

void create_mod_tables(int num) {
  fact.resize(num + 1);
  seq_inv.resize(num + 1);
  fact_inv.resize(num + 1);
  create_fact_mod(num);
  create_seq_inv_mod(num);
  create_fact_inv_mod(num);
}

int64_t comb_mod(int n, int k) {
  return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD;
}

int64_t perm_mod(int n, int k) { return fact[n] * fact_inv[n - k] % MOD; }

int main() {
  int N, X;
  cin >> N >> X;
  create_mod_tables(6010);

  static int64_t dp[3001][6010], sub[3001][6010];
  dp[0][0] = 1;
  sub[0][0] = 1;
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= 2 * N; j++) {
      for (int k = 0; k <= 2; k++)
        add(dp[i + 1][j + k], dp[i][j]);
      for (int k = 1; k <= 2; k++)
        add(sub[i + 1][j + k], sub[i][j]);
    }
  int64_t ans = 0;
  for (int j = 0; j < X; j++)
    add(ans, dp[N][j]);

  for (int t = X + 1; t <= 2 * N; t += 2) {
    int s = t + 1 - X;
    if (X % 2 == 0 && X <= s)
      break;
    if (s <= X - 1) {
      for (int d = 0; d <= N; d++) {
        int64_t result = 1;
        mul(result, sub[d][X - 1 - s]);
        int stp = s / 2 + d + (t - (X - 1)) / 2;
        if (stp > N)
          continue;
        mul(result, comb_mod(N, stp));
        add(ans, result);
      }
    } else {
      add(ans, comb_mod(N, t / 2));
    }
  }
  cout << ans << endl;
  return 0;
}