Problem p02539 (C++)

Buggy submission (status: Runtime Error):
// #define LOCAL
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int MOD = 998244353; // prime, 2^23*7*17+1
typedef int atom;
const int G = 3;

int64 power_mod(int64 a, int64 n, int p = MOD) {
  int64 ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ret * a % p;
    a = a * a % p;
  }
  return ret;
}

int64 inv_mod(int64 a) { return power_mod(a, MOD - 2); }

void bit_reverse(vector<atom> &a) {
  int n = a.size();
  for (int i = 1, j = n / 2; i < n - 1; ++i) {
    if (i < j)
      swap(a[i], a[j]);
    int k = n / 2;
    while (j >= k)
      j -= k, k /= 2;
    if (j < k)
      j += k;
  }
}

void fft(vector<atom> &a, int flag) {
  int n = a.size();
  bit_reverse(a);
  vector<atom> wn(n);
  wn[0] = 1;
  wn[1] = power_mod(G, flag == 1 ? (MOD - 1) / n : MOD - 1 - (MOD - 1) / n);
  for (int i = 2; i < n; ++i)
    wn[i] = (int64)wn[i - 1] * wn[1] % MOD;
  for (int k = 2; k <= n; k <<= 1) {
    for (int i = 0; i < n; i += k) {
      int wi = 0, step = n / k;
      for (int j = i; j < i + k / 2; ++j) {
        atom u = a[j];
        atom v = (int64)wn[wi] * a[j + k / 2] % MOD;
        a[j] = (u + v) % MOD;
        a[j + k / 2] = (u + MOD - v) % MOD;
        wi += step;
      }
    }
  }
  if (flag < 0) {
    int64 inv_n = inv_mod(n);
    for (int i = 0; i < n; ++i)
      a[i] = a[i] * inv_n % MOD;
  }
}

namespace polynomial {

vector<int> mul(const vector<int> &f, const vector<int> &g, int cap = INF) {
  int n = f.size(), m = g.size();
  int len = 1;
  while (len < n + m - 1)
    len <<= 1;
  vector<atom> x(len), y(len);
  copy(f.begin(), f.end(), x.begin());
  copy(g.begin(), g.end(), y.begin());
  fft(x, 1);
  fft(y, 1);
  for (int i = 0; i < len; ++i)
    x[i] = (int64)x[i] * y[i] % MOD;
  fft(x, -1);
  cap = min(cap, n + m - 1);
  x.resize(cap);
  return x;
}

} // namespace polynomial

using namespace polynomial;

const int N = 1e5 + 10;
int fact[N], ifact[N], inv[N];

struct comb_init {
  comb_init() {
    inv[1] = 1;
    for (int i = 2; i < N; ++i) {
      inv[i] = (MOD - MOD / i) * (int64)inv[MOD % i] % MOD;
    }
    fact[0] = ifact[0] = 1;
    for (int i = 1; i < N; ++i) {
      fact[i] = (int64)fact[i - 1] * i % MOD;
      ifact[i] = (int64)ifact[i - 1] * inv[i] % MOD;
    }
  }
} comb_init_;

int64 comb(int n, int m) {
  if (n < m || m < 0)
    return 0;
  return (int64)fact[n] * ifact[m] % MOD * ifact[n - m] % MOD;
}

void add(int &x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
}

vector<int> solve(const vector<vector<int>> &gs, int L, int R) {
  if (L + 1 == R)
    return gs[L];
  int mid = (L + R) / 2;
  auto u = solve(gs, L, mid);
  auto v = solve(gs, mid, R);
  return mul(u, v);
}

int main() {
  int n;
  cin >> n;
  vector<int> ip2(N), p2(N);
  ip2[0] = p2[0] = 1;
  for (int i = 1; i < N; ++i) {
    ip2[i] = 1LL * ip2[i - 1] * inv[2] % MOD;
    p2[i] = 1LL * p2[i - 1] * 2 % MOD;
  }
  vector<int> a(n * 2);
  for (int i = 0; i < n * 2; ++i)
    cin >> a[i];
  sort(a.begin(), a.end());
  vector<vector<int>> gs;
  for (int i = 0, j; i < n * 2; i = j) {
    for (j = i; j < n * 2 && a[j] == a[i]; ++j)
      ;
    int k = j - i;
    if (k == 1)
      continue;
    vector<int> g(k / 2 + 1);
    for (int u = 0; u <= k / 2; ++u) {
      g[u] = 1LL * fact[k] * ifact[k - 2 * u] % MOD * ifact[u] % MOD * ip2[u] %
             MOD;
    }
    gs.push_back(g);
  }
  auto f = solve(gs, 0, SZ(gs));
  trace(f);
  int ret = 0;
  for (int k = 0; k <= n && k < SZ(f); ++k) {
    int cur = f[k] % MOD;
    int cnt = 1LL * fact[2 * n - 2 * k] * ifact[n - k] % MOD * ip2[n - k] % MOD;
    cur = 1LL * cur * cnt % MOD;
    trace(k, cur);
    if (k % 2)
      cur = MOD - cur;
    add(ret, cur);
    // break;
  }
  cout << ret << '\n';
  return 0;
}


Fixed submission:
// #define LOCAL
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int MOD = 998244353; // prime, 2^23*7*17+1
typedef int atom;
const int G = 3;

int64 power_mod(int64 a, int64 n, int p = MOD) {
  int64 ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ret * a % p;
    a = a * a % p;
  }
  return ret;
}

int64 inv_mod(int64 a) { return power_mod(a, MOD - 2); }

void bit_reverse(vector<atom> &a) {
  int n = a.size();
  for (int i = 1, j = n / 2; i < n - 1; ++i) {
    if (i < j)
      swap(a[i], a[j]);
    int k = n / 2;
    while (j >= k)
      j -= k, k /= 2;
    if (j < k)
      j += k;
  }
}

void fft(vector<atom> &a, int flag) {
  int n = a.size();
  bit_reverse(a);
  vector<atom> wn(n);
  wn[0] = 1;
  wn[1] = power_mod(G, flag == 1 ? (MOD - 1) / n : MOD - 1 - (MOD - 1) / n);
  for (int i = 2; i < n; ++i)
    wn[i] = (int64)wn[i - 1] * wn[1] % MOD;
  for (int k = 2; k <= n; k <<= 1) {
    for (int i = 0; i < n; i += k) {
      int wi = 0, step = n / k;
      for (int j = i; j < i + k / 2; ++j) {
        atom u = a[j];
        atom v = (int64)wn[wi] * a[j + k / 2] % MOD;
        a[j] = (u + v) % MOD;
        a[j + k / 2] = (u + MOD - v) % MOD;
        wi += step;
      }
    }
  }
  if (flag < 0) {
    int64 inv_n = inv_mod(n);
    for (int i = 0; i < n; ++i)
      a[i] = a[i] * inv_n % MOD;
  }
}

namespace polynomial {

vector<int> mul(const vector<int> &f, const vector<int> &g, int cap = INF) {
  int n = f.size(), m = g.size();
  int len = 1;
  while (len < n + m - 1)
    len <<= 1;
  vector<atom> x(len), y(len);
  copy(f.begin(), f.end(), x.begin());
  copy(g.begin(), g.end(), y.begin());
  fft(x, 1);
  fft(y, 1);
  for (int i = 0; i < len; ++i)
    x[i] = (int64)x[i] * y[i] % MOD;
  fft(x, -1);
  cap = min(cap, n + m - 1);
  x.resize(cap);
  return x;
}

} // namespace polynomial

using namespace polynomial;

const int N = 1e5 + 10;
int fact[N], ifact[N], inv[N];

struct comb_init {
  comb_init() {
    inv[1] = 1;
    for (int i = 2; i < N; ++i) {
      inv[i] = (MOD - MOD / i) * (int64)inv[MOD % i] % MOD;
    }
    fact[0] = ifact[0] = 1;
    for (int i = 1; i < N; ++i) {
      fact[i] = (int64)fact[i - 1] * i % MOD;
      ifact[i] = (int64)ifact[i - 1] * inv[i] % MOD;
    }
  }
} comb_init_;

int64 comb(int n, int m) {
  if (n < m || m < 0)
    return 0;
  return (int64)fact[n] * ifact[m] % MOD * ifact[n - m] % MOD;
}

void add(int &x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
}

vector<int> solve(const vector<vector<int>> &gs, int L, int R) {
  if (L == R)
    return {1};
  if (L + 1 == R)
    return gs[L];
  int mid = (L + R) / 2;
  auto u = solve(gs, L, mid);
  auto v = solve(gs, mid, R);
  return mul(u, v);
}

int main() {
  int n;
  cin >> n;
  vector<int> ip2(N), p2(N);
  ip2[0] = p2[0] = 1;
  for (int i = 1; i < N; ++i) {
    ip2[i] = 1LL * ip2[i - 1] * inv[2] % MOD;
    p2[i] = 1LL * p2[i - 1] * 2 % MOD;
  }
  vector<int> a(n * 2);
  for (int i = 0; i < n * 2; ++i)
    cin >> a[i];
  sort(a.begin(), a.end());
  vector<vector<int>> gs;
  for (int i = 0, j; i < n * 2; i = j) {
    for (j = i; j < n * 2 && a[j] == a[i]; ++j)
      ;
    int k = j - i;
    if (k == 1)
      continue;
    vector<int> g(k / 2 + 1);
    for (int u = 0; u <= k / 2; ++u) {
      g[u] = 1LL * fact[k] * ifact[k - 2 * u] % MOD * ifact[u] % MOD * ip2[u] %
             MOD;
    }
    gs.push_back(g);
  }
  auto f = solve(gs, 0, SZ(gs));
  trace(f);
  int ret = 0;
  for (int k = 0; k <= n && k < SZ(f); ++k) {
    int cur = f[k] % MOD;
    int cnt = 1LL * fact[2 * n - 2 * k] * ifact[n - k] % MOD * ip2[n - k] % MOD;
    cur = 1LL * cur * cnt % MOD;
    trace(k, cur);
    if (k % 2)
      cur = MOD - cur;
    add(ret, cur);
    // break;
  }
  cout << ret << '\n';
  return 0;
}