Problem p03622 (C++)

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

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 998244353;
const int maxn = 2e5 + 5;

int n, m, ans = 0;
int f[maxn], g[maxn], pw[maxn], fac[maxn], ifac[maxn];

inline int qpow(int x, int y);
inline int add(int x, int y) { return (x + y) % INF; }
inline int mul(int x, int y) { return 1ll * x * y % INF; }
inline int sub(int x, int y) { return (x - y + INF) % INF; }
inline void uadd(int &x, int y) { (x += y) %= INF; }
inline void usub(int &x, int y) { ((x += INF) -= y) %= INF; }
inline void umul(int &x, int y) { x = 1ll * x * y % INF; }
inline void udiv(int &x, int y) { umul(x, qpow(y, INF - 2)); }

inline int qpow(int x, int y) {
  int ret = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      umul(ret, x);
    umul(x, x);
  }
  return ret;
}

inline int binom(int x, int y) {
  return mul(fac[x], mul(ifac[y], ifac[x - y]));
}

inline int solve() {
  int ret = 0;
  REP(i, 0, min(n - 1, m)) {
    if (n - 1 == m) {
      if (i == n - 1)
        f[i] = 1;
      else
        f[i] = 0;
    } else if (n - 1 < m)
      f[i] = sub(binom((n - 1) - i + m - (i + 1), m - (i + 1)),
                 binom((n - 1) - (i + 1) + m - i, m - i));
    else {
      f[i] = sub(binom((n - 1) - (i + 1) + m - i, m - i),
                 binom((n - 1) - i + m - (i + 1), m - (i + 1)));
      uadd(ans, mul(mul(f[i], binom(2 * i, i)), n + m - i * 2 - 1));
    }
    g[i] = mul(binom(i * 2, i), i * 2 + 1);
    uadd(g[i], pw[i]);
    udiv(g[i], 2);
    uadd(ret, mul(f[i], g[i]));
  }
  return ret;
}

int main() {
  scanf("%d%d", &n, &m);
  fac[0] = 1;
  FOR(i, 1, maxn)
  fac[i] = mul(fac[i - 1], i);
  ifac[maxn - 1] = qpow(fac[maxn - 1], INF - 2);
  for (int i = maxn - 2; i >= 0; --i)
    ifac[i] = mul(ifac[i + 1], i + 1);
  pw[0] = 1;
  FOR(i, 1, maxn)
  pw[i] = mul(pw[i - 1], 4);

  uadd(ans, solve());
  swap(n, m);
  uadd(ans, solve());

  REP(i, 0, min(n, m)) {
    uadd(ans, mul(binom(i * 2 - 1, i), binom(n + m - 2 * i, n - i)));
  }
  udiv(ans, binom(n + m, m));

  printf("%d\n", ans);
  return 0;
}


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

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 998244353;
const int maxn = 1e6 + 5;

int n, m, ans = 0;
int f[maxn], g[maxn], pw[maxn], fac[maxn], ifac[maxn];

inline int qpow(int x, int y);
inline int add(int x, int y) { return (x + y) % INF; }
inline int mul(int x, int y) { return 1ll * x * y % INF; }
inline int sub(int x, int y) { return (x - y + INF) % INF; }
inline void uadd(int &x, int y) { (x += y) %= INF; }
inline void usub(int &x, int y) { ((x += INF) -= y) %= INF; }
inline void umul(int &x, int y) { x = 1ll * x * y % INF; }
inline void udiv(int &x, int y) { umul(x, qpow(y, INF - 2)); }

inline int qpow(int x, int y) {
  int ret = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      umul(ret, x);
    umul(x, x);
  }
  return ret;
}

inline int binom(int x, int y) {
  return mul(fac[x], mul(ifac[y], ifac[x - y]));
}

inline int solve() {
  int ret = 0;
  REP(i, 0, min(n - 1, m)) {
    if (n - 1 == m) {
      if (i == n - 1)
        f[i] = 1;
      else
        f[i] = 0;
    } else if (n - 1 < m)
      f[i] = sub(binom((n - 1) - i + m - (i + 1), m - (i + 1)),
                 binom((n - 1) - (i + 1) + m - i, m - i));
    else {
      f[i] = sub(binom((n - 1) - (i + 1) + m - i, m - i),
                 binom((n - 1) - i + m - (i + 1), m - (i + 1)));
      uadd(ans, mul(mul(f[i], binom(2 * i, i)), n + m - i * 2 - 1));
    }
    g[i] = mul(binom(i * 2, i), i * 2 + 1);
    uadd(g[i], pw[i]);
    udiv(g[i], 2);
    uadd(ret, mul(f[i], g[i]));
  }
  return ret;
}

int main() {
  scanf("%d%d", &n, &m);
  fac[0] = 1;
  FOR(i, 1, maxn)
  fac[i] = mul(fac[i - 1], i);
  ifac[maxn - 1] = qpow(fac[maxn - 1], INF - 2);
  for (int i = maxn - 2; i >= 0; --i)
    ifac[i] = mul(ifac[i + 1], i + 1);
  pw[0] = 1;
  FOR(i, 1, maxn)
  pw[i] = mul(pw[i - 1], 4);

  uadd(ans, solve());
  swap(n, m);
  uadd(ans, solve());

  REP(i, 0, min(n, m)) {
    uadd(ans, mul(binom(i * 2 - 1, i), binom(n + m - 2 * i, n - i)));
  }
  udiv(ans, binom(n + m, m));

  printf("%d\n", ans);
  return 0;
}