Problem p02539 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fi first
#define se second
#define lc (x << 1)
#define rc (x << 1 | 1)
#define gc                                                                     \\
  getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define vi vector<int>
#define TP template <class o>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

// char buf[size],*p1=buf,*p2=buf;
TP void qr(o &x) {
  char c = gc;
  x = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = gc;
  }
  while (isdigit(c))
    x = x * 10 + c - '0', c = gc;
  x *= f;
}
TP void qw(o x) {
  if (x / 10)
    qw(x / 10);
  putchar(x % 10 + '0');
}
TP void pr1(o x) {
  if (x < 0)
    x = -x, putchar('-');
  qw(x);
  putchar(' ');
}
TP void pr2(o x) {
  if (x < 0)
    x = -x, putchar('-');
  qw(x);
  putchar('\\n');
}

// math
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll a, ll b = mod - 2) {
  ll c = 1;
  while (b) {
    if (b & 1)
      c = c * a % mod;
    b /= 2;
    a = a * a % mod;
  }
  return c;
}

TP void cmax(o &x, o y) {
  if (x < y)
    x = y;
}
TP void cmin(o &x, o y) {
  if (x > y)
    x = y;
}

// dbinom

ll jc[N], inv[N];
void init(int n) {
  jc[0] = 1;
  for (int i = 1; i <= n; i++)
    jc[i] = jc[i - 1] * i % mod;
  inv[n] = power(jc[n]);
  for (int i = n; i; i--)
    inv[i - 1] = inv[i] * i % mod;
}
ll C(int x, int y) {
  if (x < y || y < 0)
    return 0;
  return jc[x] * inv[y] % mod * inv[x - y] % mod;
}

int n, a[N], sum, m;
ll f[N], g[N], t[N];

void solve() {
  qr(n);
  m = 2 * n;
  for (int i = 1, x; i <= m; i++)
    qr(x), a[x]++;
  f[0] = g[0] = 1;
  init(2 * n);
  for (int i = 1, j = 1; i <= n; i++)
    g[i] = (ll)j * (j + 1) / 2 % mod * g[i - 1] % mod, j += 2;
  for (int i = 1; i <= n; i++)
    g[i] = g[i] * inv[i] % mod;
  for (int i = 1, len; i < N; i++)
    if (a[i] > 1) {
      len = a[i] / 2;
      for (int k = 1; k <= len; k++)
        t[k] = C(a[i], 2 * k) * g[k] % mod;
      for (int j = sum; j >= 0; j--)
        for (int k = 1; k <= len; k++)
          f[j + k] = (f[j + k] + f[j] * t[k]) % mod;
      sum += len;
    }
  ll ans = 0;
  for (int i = 0; i <= n; i++)
    ans += (i & 1 ? mod - 1 : 1) * f[i] % mod * g[n - i] % mod;
  pr2((ans % mod + mod) % mod);
}

int main() {
  int T = 1;
  //	qr(T);
  while (T--)
    solve();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define lc (x << 1)
#define rc (x << 1 | 1)
#define gc                                                                     \\
  getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define vi vector<int>
#define TP template <class o>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9;

// char buf[size],*p1=buf,*p2=buf;
template <class o> void qr(o &x) {
  char c = gc;
  x = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = gc;
  }
  while (isdigit(c))
    x = x * 10 + c - '0', c = gc;
  x *= f;
}
template <class o> void qw(o x) {
  if (x / 10)
    qw(x / 10);
  putchar(x % 10 + '0');
}
template <class o> void pr1(o x) {
  if (x < 0)
    x = -x, putchar('-');
  qw(x);
  putchar(' ');
}
template <class o> void pr2(o x) {
  if (x < 0)
    x = -x, putchar('-');
  qw(x);
  putchar('\\n');
}
ll power(ll a, ll b = mod - 2) {
  ll c = 1;
  while (b) {
    if (b & 1)
      c = c * a % mod;
    b /= 2;
    a = a * a % mod;
  }
  return c;
}

namespace P {
const int g = 3, inv2 = (mod + 1) / 2;
int R[N], w[N], Inv[N];
int calc(int x) {
  if ((x & -x) == x)
    return x;
  int n = 1;
  while (n < x)
    n *= 2;
  return n;
} // ÊäÈë³¤¶È
void init(int m) {
  int n = calc(m) * 4;
  Inv[1] = 1;
  for (int i = 2; i < n; i++)
    Inv[i] = (ll)Inv[mod % i] * (mod - mod / i) % mod;
  for (int i = 1; i < n; i *= 2) { // Ã¶¾Ù°ëÇø¼ä³¤¶È,°Ñ¶ÔÓ¦µÄµ¥Î»¸ùÌîÈëwÊý×é
    ll t = power(g, (mod - 1) / (2 * i)), d = 1;
    for (int j = 0; j < i; j++)
      w[i + j] = d, d = d * t % mod;
  }
}
int pre(int m) { // ÊäÈë×Ü³¤¶È
  int n = calc(m);
  for (int i = 1; i < n; i++)
    R[i] = (R[i >> 1] >> 1) | (i & 1 ? n >> 1 : 0);
  return n;
}
void upd(int &x) { x += x >> 31 & mod; }
ll power(ll a, ll b = mod - 2) {
  ll c = 1;
  while (b) {
    if (b & 1)
      c = c * a % mod;
    b /= 2;
    a = a * a % mod;
  }
  return c;
}
void DFT(int *f, int n) {
  static ull p[N];
  for (int i = 0; i < n; i++)
    p[R[i]] = f[i];
  for (int i = 1, t; i < n; i *= 2)
    for (int j = 0; j < n; j += 2 * i)
      for (int k = 0; k < i; k++)
        t = p[j + k + i] * w[i + k] % mod, p[j + k + i] = p[j + k] + mod - t,
        p[j + k] += t;
  for (int i = 0; i < n; i++)
    f[i] = p[i] % mod;
}
void IDFT(int *f, int n) {
  reverse(f + 1, f + n);
  DFT(f, n);
  ll inv = power(n);
  for (int i = 0; i < n; i++)
    f[i] = inv * f[i] % mod;
}
void copy(int *a, int *b, int n) { memcpy(a, b, sizeof(int) * n); }
void clear(int *a, int len) { memset(a + len, 0, sizeof(int) * len); }
void clear(int *a, int x, int y) {
  if (x < y)
    memset(a + x, 0, sizeof(int) * (y - x));
}
void dao(int *a, int *b, int n) {
  for (int i = 1; i < n; i++)
    b[i - 1] = (ll)a[i] * i % mod;
  b[n - 1] = 0;
}
void ji(int *a, int *b, int n) {
  for (int i = n - 1; i; i--)
    b[i] = (ll)a[i - 1] * Inv[i] % mod;
  b[0] = 0;
}
void mult(int *a, int *b, int &n, int m) {
  static int c[N], d[N];
  int x = pre(n + m);
  copy(d, a, n);
  clear(d, n, x);
  copy(c, b, m);
  clear(c, m, x);
  DFT(c, x);
  DFT(d, x);
  for (int i = 0; i < x; i++)
    c[i] = (ll)c[i] * d[i] % mod;
  IDFT(c, x);
  n += m - 1;
  for (int i = 0; i < n; i++)
    a[i] = c[i];
}
int h[N];
void getinv(int *a, int *b, int n) { //
  clear(b, 0, 2 * n);
  clear(a, n);
  clear(h, 0, 2 * n);
  b[0] = power(a[0]);
  for (int p = 2; p <= n; p *= 2) {
    int x = pre(p * 2);
    copy(h, a, p);
    clear(h, p);
    DFT(h, x);
    DFT(b, x);
    for (int i = 0; i < x; i++)
      b[i] = (ll)(2 - (ll)b[i] * h[i] % mod + mod) * b[i] % mod;
    IDFT(b, x);
    clear(b, p);
  }
}
void getsqrt(int *a, int *b, int n) {
  static int c[N], f[N];
  clear(c, 0, 2 * n);
  clear(f, 0, 2 * n);
  clear(a, n);
  clear(b, 0, 2 * n);
  b[0] = power(a[0]);
  b[1] = 0;
  for (int p = 2; p <= n; p *= 2) {
    copy(f, a, p);
    clear(f, p);
    getinv(b, c, p);
    mult(f, c, p, p);
    for (int i = 0; i < p; i++)
      b[i] = (ll)(b[i] + f[i]) * inv2 % mod;
  }
}
void getln(int *a, int *b, int n) { //
  getinv(a, b, n);
  dao(a, h, n);
  mult(h, b, n, n);
  ji(h, b, n);
  clear(h, 0, 2 * n);
}
void getexp(int *a, int *b, int n) {
  static int c[N];
  clear(b, 0, 2 * n);
  clear(a, n);
  b[0] = 1;
  for (int p = 2; p <= n; p *= 2) {
    copy(c, b, p / 2);
    clear(c, p / 2);
    getln(c, b, p);
    upd(--b[0]);
    for (int i = 0; i < p; i++)
      upd(b[i] = -b[i] + a[i]);
    mult(b, c, p, p);
    clear(b, p);
  }
}
void getdiv(int *a, int *b, int *c, int n, int m) { // c=a(n)/b(m).
  static int h1[N], h2[N];
  int len = n - m + 1, x = calc(len);
  copy(h1, a, n);
  copy(h2, b, m);
  clear(h1, n, x);
  clear(h2, m, x);
  reverse(h1, h1 + n);
  reverse(h2, h2 + m);
  getinv(h2, c, x);
  mult(c, h1, x, x);
  reverse(c, c + len);
  clear(c, len);
}
} // namespace P

int n, tot, F[N], *f[N], *fp = F, sz[N];
map<int, int> c;

ll jc[N], inv[N];
void init(int n) {
  jc[0] = 1;
  for (int i = 1; i <= n; i++)
    jc[i] = jc[i - 1] * i % mod;
  inv[n] = power(jc[n]);
  for (int i = n; i; i--)
    inv[i - 1] = inv[i] * i % mod;
}
ll C(int x, int y) {
  if (x < y || y < 0)
    return 0;
  return jc[x] * inv[y] % mod * inv[x - y] % mod;
}

void solve(int l, int r) {
  if (l == r)
    return;
  int mid = (l + r) / 2;
  solve(l, mid);
  solve(mid + 1, r);
  P::mult(f[l], f[mid + 1], sz[l], sz[mid + 1]);
}

int main() {
  qr(n);
  init(n);
  P::init(n);
  for (int i = 1, x; i <= n * 2; i++) {
    qr(x);
    c[x]++;
  }
  for (pii i : c) {
    int len = i.se;
    sz[++tot] = len;
    f[tot] = fp;
    fp += len + 1;
    for (int i = 0; i <= len / 2; i++)
      f[tot][i] =
          C(len, 2 * i) * jc[2 * i] % mod * power((mod + 1) / 2, i) % mod;
  }
  solve(1, tot);
  ll ans = 0;
  for (int i = 0; i <= n; i++)
    ans += (i & 1 ? -1 : 1) * f[1][i] % mod * jc[2 * n - 2 * i] % mod *
           power((mod + 1) / 2, (n - i)) % mod;
  pr2((ans % mod + mod) % mod);
}