Problem p03066 (C++)

Buggy submission (status: Runtime Error):
/* --- author: dxm --- */
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = 10 * x + c - '0', c = getchar();
  x *= neg;
}

const int maxn = 3005;
const int mod = 998244353;
int n, x;
int fac[maxn * 2], ifac[maxn * 2];
int C(int a, int b) {
  return a < b ? 0 : 1LL * fac[a] * ifac[b] % mod * ifac[a - b] % mod;
}

int main() {
#ifdef FILE
  freopen("FILE.in", "r", stdin);
  freopen("FILE.out", "w", stdout);
#endif
  scanf("%d%d", &n, &x);
  fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
  for (int i = 2; i <= 2 * n; i++) {
    fac[i] = 1LL * fac[i - 1] * i % mod;
    ifac[i] = 1LL * (mod - mod / i) * ifac[mod % i] % mod;
  }
  REP(i, 2 * n) ifac[i] = 1LL * ifac[i - 1] * ifac[i] % mod;
  int tot = 0;
  rep(i, n + 1) {
    int ans = 0;
    rep(s, x) ans = (ans + C(i, s - i)) % mod;
    rep(j, i) {
      int l = i - j + 1, r = j;
      if (l > r) {
        if (x - 1 == 2 * j)
          ans = (ans + 1) % mod;
        continue;
      }
      int s = x - 1 - 2 * (i - j);
      if (s < 0)
        continue;
      ans = (ans + C(r - l + 1, s - (r - l + 1))) % mod;
    }
    tot = (tot + 1LL * ans * C(n, i) % mod) % mod;
  }
  printf("%d\n", tot);
#ifdef TIME
  printf("Running Time = %d ms\n", int(clock() * 1000.0 / CLOCKS_PER_SEC));
#endif
  return 0;
}

/*
Input:
-----------------
Output:
*/


Fixed submission:
/* --- author: dxm --- */
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = 10 * x + c - '0', c = getchar();
  x *= neg;
}

const int maxn = 3005;
const int mod = 998244353;
int n, x;
int fac[maxn * 2], ifac[maxn * 2];
int C(int a, int b) {
  return (b < 0 || a < b) ? 0
                          : 1LL * fac[a] * ifac[b] % mod * ifac[a - b] % mod;
}

int main() {
#ifdef FILE
  freopen("FILE.in", "r", stdin);
  freopen("FILE.out", "w", stdout);
#endif
  scanf("%d%d", &n, &x);
  fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
  for (int i = 2; i <= 2 * n; i++) {
    fac[i] = 1LL * fac[i - 1] * i % mod;
    ifac[i] = 1LL * (mod - mod / i) * ifac[mod % i] % mod;
  }
  REP(i, 2 * n) ifac[i] = 1LL * ifac[i - 1] * ifac[i] % mod;
  int tot = 0;
  rep(i, n + 1) {
    int ans = 0;
    rep(s, x) ans = (ans + C(i, s - i)) % mod;
    rep(j, i) {
      int l = i - j + 1, r = j;
      if (l > r) {
        if (x - 1 == 2 * j)
          ans = (ans + 1) % mod;
        continue;
      }
      int s = x - 1 - 2 * (i - j);
      if (s < 0)
        continue;
      ans = (ans + C(r - l + 1, s - (r - l + 1))) % mod;
    }
    tot = (tot + 1LL * ans * C(n, i) % mod) % mod;
  }
  printf("%d\n", tot);
#ifdef TIME
  printf("Running Time = %d ms\n", int(clock() * 1000.0 / CLOCKS_PER_SEC));
#endif
  return 0;
}

/*
Input:
-----------------
Output:
*/