Problem p03431 (C++)

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

#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-funroll-loops")
#pragma GCC target("avx,sse4")

#define mms(a, n) memset(a, 0, sizeof((a)[0]) * (n))
#define mmp(a, b, n) memcpy(a, b, sizeof((b)[0]) * (n))
#define lowbit(x) ((x) & -(x))
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fo(i, l, r) for (register int i = l, _lim_ = r; i <= _lim_; i++)
#define fd(i, r, l) for (register int i = r, _lim_ = l; i >= _lim_; i--)
#define fos(i, l, r, d) for (register int i = l, _lim_ = r; i <= r; i += d)
#define fol(i, l, r) for (register ll i = l, _lim_ = r; i <= _lim_; i++)
#define fdl(i, r, l) for (register ll i = r, _lim_ = l; i >= _lim_; i--)
#define fosl(i, l, r, d) for (register ll i = l, _lim_ = r; i <= r; i += d)
#define Clear(a) memset(a, 0, sizeof(a))
#define Copy(a, b) memcpy(a, b, sizeof(b))
#define ALL(v) v.begin(), v.end()
#define SZ(v) ((int)v.size())

using namespace std;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ldb;
typedef double db;
typedef pair<int, int> pi;
typedef vector<int> VI;
typedef vector<VI> VII;

const int N = 500005, P = 998244353;

int fpow(int a, int t) {
  static int r;
  for (r = 1; t; t >>= 1, a = (ll)a * a % P)
    if (t & 1)
      r = (ll)r * a % P;
  return r;
}

int n, m, k, fac[N], ifac[N], ret;
int C(int n, int m) {
  return n < m || m < 0 ? 0 : (ll)fac[n] * ifac[m] % P * ifac[n - m] % P;
}
int G(int x, int y) { return C(x, (x + y) / 2); }
int S(int x, int y, int z) {
  return z >= 0 ? 0 : (G(x, y) - G(x, 2 * z - y) + P) % P;
}

int main() {
  scanf("%d%d", &n, &k);
  m = k << 1;
  fac[0] = 1;
  fo(i, 1, m) fac[i] = (ll)fac[i - 1] * i % P;
  ifac[m] = fpow(fac[m], P - 2);
  fd(i, m, 1) ifac[i - 1] = (ll)ifac[i] * i % P;
  fo(red, 1, k) {
    int blue = k - red;
    if (red < blue)
      continue;
    if (red == blue)
      ret = (ret + S(k - 1, 1, -k / 2 + n - 1)) % P;
    if (red > blue)
      ret = (ret + S(red + blue, red - blue, n - red - 1)) % P;
  }
  printf("%d\n", ret);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-funroll-loops")
#pragma GCC target("avx,sse4")

#define mms(a, n) memset(a, 0, sizeof((a)[0]) * (n))
#define mmp(a, b, n) memcpy(a, b, sizeof((b)[0]) * (n))
#define lowbit(x) ((x) & -(x))
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fo(i, l, r) for (register int i = l, _lim_ = r; i <= _lim_; i++)
#define fd(i, r, l) for (register int i = r, _lim_ = l; i >= _lim_; i--)
#define fos(i, l, r, d) for (register int i = l, _lim_ = r; i <= r; i += d)
#define fol(i, l, r) for (register ll i = l, _lim_ = r; i <= _lim_; i++)
#define fdl(i, r, l) for (register ll i = r, _lim_ = l; i >= _lim_; i--)
#define fosl(i, l, r, d) for (register ll i = l, _lim_ = r; i <= r; i += d)
#define Clear(a) memset(a, 0, sizeof(a))
#define Copy(a, b) memcpy(a, b, sizeof(b))
#define ALL(v) v.begin(), v.end()
#define SZ(v) ((int)v.size())

using namespace std;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ldb;
typedef double db;
typedef pair<int, int> pi;
typedef vector<int> VI;
typedef vector<VI> VII;

const int N = 1000005, P = 998244353;

int fpow(int a, int t) {
  static int r;
  for (r = 1; t; t >>= 1, a = (ll)a * a % P)
    if (t & 1)
      r = (ll)r * a % P;
  return r;
}

int n, m, k, fac[N], ifac[N], ret;
int C(int n, int m) {
  return n < m || m < 0 ? 0 : (ll)fac[n] * ifac[m] % P * ifac[n - m] % P;
}
int G(int x, int y) { return C(x, (x + y) / 2); }
int S(int x, int y, int z) {
  return z >= 0 ? 0 : (G(x, y) - G(x, 2 * z - y) + P) % P;
}

int main() {
  scanf("%d%d", &n, &k);
  m = k << 1;
  fac[0] = 1;
  fo(i, 1, m) fac[i] = (ll)fac[i - 1] * i % P;
  ifac[m] = fpow(fac[m], P - 2);
  fd(i, m, 1) ifac[i - 1] = (ll)ifac[i] * i % P;
  fo(red, 1, k) {
    int blue = k - red;
    if (red < blue)
      continue;
    if (red == blue)
      ret = (ret + S(k - 1, 1, -k / 2 + n - 1)) % P;
    if (red > blue)
      ret = (ret + S(red + blue, red - blue, n - red - 1)) % P;
  }
  printf("%d\n", ret);
  return 0;
}