Problem p02539 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
constexpr int inf = 1e9 + 7;
constexpr ll longinf = 1LL << 60;
constexpr ll mod = 998244353;

vector<ll> inv, fact, invfact;
void mod_build(int n = 101010) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  invfact.resize(n + 1);
  fact[0] = inv[0] = invfact[0] = 1;
  inv[1] = 1;
  rep(i, n) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    if (i > 0)
      inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    invfact[i + 1] = invfact[i] * inv[i + 1] % mod;
  }
}
ll perm(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return fact[n] * invfact[n - k] % mod;
}
ll comb(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return (fact[n] * invfact[n - k] % mod) * invfact[k] % mod;
}
ll powmod(ll n, ll k) {
  k %= mod - 1;
  if (k < 0)
    k += mod - 1;
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return ret;
}

struct NumberTheoreticTransform {
  int mod;
  int primitiveroot;

  NumberTheoreticTransform(int mod, int root) : mod(mod), primitiveroot(root) {}

  inline int mod_pow(int x, int n) {
    int ret = 1;
    while (n > 0) {
      if (n & 1)
        ret = mul(ret, x);
      x = mul(x, x);
      n >>= 1;
    }
    return ret;
  }

  inline int inverse(int x) { return (mod_pow(x, mod - 2)); }

  inline int add(unsigned x, int y) {
    x += y;
    if (x >= mod)
      x -= mod;
    return (x);
  }

  inline int mul(int a, int b) {
    unsigned long long x = (long long)a * b;
    unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
    asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod));
    return (m);
  }

  void DiscreteFourierTransform(vector<int> &F, bool rev) {
    const int N = (int)F.size();
    for (int i = 0, j = 1; j + 1 < N; j++) {
      for (int k = N >> 1; k > (i ^= k); k >>= 1)
        ;
      if (i > j)
        swap(F[i], F[j]);
    }
    int w, wn, s, t;
    for (int i = 1; i < N; i <<= 1) {
      w = mod_pow(primitiveroot, (mod - 1) / (i * 2));
      if (rev)
        w = inverse(w);
      for (int k = 0; k < i; k++) {
        wn = mod_pow(w, k);
        for (int j = 0; j < N; j += i * 2) {
          s = F[j + k], t = mul(F[j + k + i], wn);
          F[j + k] = add(s, t), F[j + k + i] = add(s, mod - t);
        }
      }
    }
    if (rev) {
      int temp = inverse(N);
      for (int i = 0; i < N; i++)
        F[i] = mul(F[i], temp);
    }
  }

  vector<int> Multiply(const vector<int> &A, const vector<int> &B) {
    int sz = 1;
    while (sz < A.size() + B.size() - 1)
      sz <<= 1;
    vector<int> F(sz), G(sz);
    for (int i = 0; i < A.size(); i++)
      F[i] = A[i];
    for (int i = 0; i < B.size(); i++)
      G[i] = B[i];
    DiscreteFourierTransform(F, false);
    DiscreteFourierTransform(G, false);
    for (int i = 0; i < sz; i++)
      F[i] = mul(F[i], G[i]);
    DiscreteFourierTransform(F, true);
    F.resize(A.size() + B.size() - 1);
    return (F);
  }
};

struct P {
  int x;
  vector<int> v;
  bool operator>(const P &rhs) const { return x > rhs.x; }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> cnt(101010);
  rep(i, 2 * n) {
    int x;
    cin >> x;
    cnt[x]++;
  }
  mod_build();
  priority_queue<P, vector<P>, greater<P>> pq;
  rep(i, 101010) {
    int m = cnt[i];
    if (m < 2)
      continue;
    vector<int> a(m / 2 + 1);
    ll ret = 1;
    rep(j, m / 2 + 1) {
      a[j] = ret;
      ret *= comb(m - 2 * j, 2);
      ret %= mod;
      ret *= inv[j + 1];
      ret %= mod;
    }
    pq.push({m, a});
  }
  auto ntt = NumberTheoreticTransform(mod, 3);
  while (pq.size() > 1) {
    auto p1 = pq.top();
    pq.pop();
    auto p2 = pq.top();
    pq.pop();
    auto res = ntt.Multiply(p1.v, p2.v);
    pq.push({(int)res.size(), res});
  }
  auto ret = pq.top().v;
  ll ans = 0;
  rep(i, ret.size()) {
    int sz = 2 * n - 2 * i;
    ll cnt =
        fact[sz] * invfact[sz / 2] % mod * powmod(2, mod - 1 - sz / 2) % mod;
    if (i % 2)
      ans += mod - ret[i] * cnt % mod;
    else
      ans += ret[i] * cnt % mod;
  }
  cout << ans % mod << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
constexpr int inf = 1e9 + 7;
constexpr ll longinf = 1LL << 60;
constexpr ll mod = 998244353;

vector<ll> inv, fact, invfact;
void mod_build(int n = 101010) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  invfact.resize(n + 1);
  fact[0] = inv[0] = invfact[0] = 1;
  inv[1] = 1;
  rep(i, n) {
    fact[i + 1] = fact[i] * (i + 1) % mod;
    if (i > 0)
      inv[i + 1] = mod - inv[mod % (i + 1)] * (mod / (i + 1)) % mod;
    invfact[i + 1] = invfact[i] * inv[i + 1] % mod;
  }
}
ll perm(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return fact[n] * invfact[n - k] % mod;
}
ll comb(int n, int k) {
  if (n < 0 || k < 0 || k > n)
    return 0;
  return (fact[n] * invfact[n - k] % mod) * invfact[k] % mod;
}
ll powmod(ll n, ll k) {
  k %= mod - 1;
  if (k < 0)
    k += mod - 1;
  ll ret = 1;
  while (k) {
    if (k & 1)
      ret = ret * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return ret;
}

struct NumberTheoreticTransform {
  int mod;
  int primitiveroot;

  NumberTheoreticTransform(int mod, int root) : mod(mod), primitiveroot(root) {}

  inline int mod_pow(int x, int n) {
    int ret = 1;
    while (n > 0) {
      if (n & 1)
        ret = mul(ret, x);
      x = mul(x, x);
      n >>= 1;
    }
    return ret;
  }

  inline int inverse(int x) { return (mod_pow(x, mod - 2)); }

  inline int add(unsigned x, int y) {
    x += y;
    if (x >= mod)
      x -= mod;
    return (x);
  }

  inline int mul(int a, int b) {
    unsigned long long x = (long long)a * b;
    unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
    asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod));
    return (m);
  }

  void DiscreteFourierTransform(vector<int> &F, bool rev) {
    const int N = (int)F.size();
    for (int i = 0, j = 1; j + 1 < N; j++) {
      for (int k = N >> 1; k > (i ^= k); k >>= 1)
        ;
      if (i > j)
        swap(F[i], F[j]);
    }
    int w, wn, s, t;
    for (int i = 1; i < N; i <<= 1) {
      w = mod_pow(primitiveroot, (mod - 1) / (i * 2));
      if (rev)
        w = inverse(w);
      for (int k = 0; k < i; k++) {
        wn = mod_pow(w, k);
        for (int j = 0; j < N; j += i * 2) {
          s = F[j + k], t = mul(F[j + k + i], wn);
          F[j + k] = add(s, t), F[j + k + i] = add(s, mod - t);
        }
      }
    }
    if (rev) {
      int temp = inverse(N);
      for (int i = 0; i < N; i++)
        F[i] = mul(F[i], temp);
    }
  }

  vector<int> Multiply(const vector<int> &A, const vector<int> &B) {
    int sz = 1;
    while (sz < A.size() + B.size() - 1)
      sz <<= 1;
    vector<int> F(sz), G(sz);
    for (int i = 0; i < A.size(); i++)
      F[i] = A[i];
    for (int i = 0; i < B.size(); i++)
      G[i] = B[i];
    DiscreteFourierTransform(F, false);
    DiscreteFourierTransform(G, false);
    for (int i = 0; i < sz; i++)
      F[i] = mul(F[i], G[i]);
    DiscreteFourierTransform(F, true);
    F.resize(A.size() + B.size() - 1);
    return (F);
  }
};

struct P {
  int x;
  vector<int> v;
  bool operator>(const P &rhs) const { return x > rhs.x; }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> cnt(101010);
  rep(i, 2 * n) {
    int x;
    cin >> x;
    cnt[x]++;
  }
  mod_build();
  priority_queue<P, vector<P>, greater<P>> pq;
  rep(i, 101010) {
    int m = cnt[i];
    if (m < 2)
      continue;
    vector<int> a(m / 2 + 1);
    ll ret = 1;
    rep(j, m / 2 + 1) {
      a[j] = ret;
      ret *= comb(m - 2 * j, 2);
      ret %= mod;
      ret *= inv[j + 1];
      ret %= mod;
    }
    pq.push({m, a});
  }
  pq.push({0, {1}});
  auto ntt = NumberTheoreticTransform(mod, 3);
  while (pq.size() > 1) {
    auto p1 = pq.top();
    pq.pop();
    auto p2 = pq.top();
    pq.pop();
    auto res = ntt.Multiply(p1.v, p2.v);
    pq.push({(int)res.size(), res});
  }
  auto ret = pq.top().v;
  ll ans = 0;
  rep(i, ret.size()) {
    int sz = 2 * n - 2 * i;
    ll cnt =
        fact[sz] * invfact[sz / 2] % mod * powmod(2, mod - 1 - sz / 2) % mod;
    if (i % 2)
      ans += mod - ret[i] * cnt % mod;
    else
      ans += ret[i] * cnt % mod;
  }
  cout << ans % mod << endl;
  return 0;
}