Problem p02539 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define Per(i, sta, n) for (int i = n - 1; i >= sta; i--)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <int mod> struct ModInt {
  long long x;

  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  explicit operator int() const { return x; }

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return ModInt(u);
  }

  ModInt power(long long p) const {
    int a = x;
    if (p == 0)
      return 1;
    if (p == 1)
      return ModInt(a);
    if (p % 2 == 1)
      return (ModInt(a) * ModInt(a)).power(p / 2) * ModInt(a);
    else
      return (ModInt(a) * ModInt(a)).power(p / 2);
  }

  ModInt power(const ModInt p) const { return ((ModInt)x).power(p.x); }

  friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};

using modint = ModInt<mod>;

struct ModFac {
public:
  vector<modint> f, i_f;
  int n;

  ModFac(int n_) {
    n = n_;
    f.resize(n + 1, 1);
    i_f.resize(n + 1, 1);
    for (int i = 0; i < n; i++) {
      f[i + 1] = f[i] * (modint)(i + 1);
    }
    i_f[n] = f[n].power(mod - 2);
    for (int i = n - 1; i >= 0; i--) {
      i_f[i] = i_f[i + 1] * (modint)(i + 1);
    }
  }
  ModFac(modint n_) {
    n = (int)n_;
    f.resize(n + 1, 1);
    i_f.resize(n + 1, 1);
    for (int i = 0; i < n; i++) {
      f[i + 1] = f[i] * (modint)(i + 1);
    }
    i_f[n] = f[n].power(mod - 2);
    for (int i = n - 1; i >= 0; i--) {
      i_f[i] = i_f[i + 1] * (modint)(i + 1);
    }
  }

  modint factorial(int x) {
    // cout << f.size() << endl;
    return f[x];
  }

  modint inv_factorial(int x) { return i_f[x]; }

  modint comb(int m, int k) {
    if (m < 0 or k < 0)
      return 0;
    if (m < k)
      return 0;
    return f[m] * i_f[k] * i_f[m - k];
  }
};

constexpr int bmds(int x) {
  const int v[] = {1012924417, 924844033, 998244353, 897581057, 645922817};
  return v[x];
}
constexpr int brts(int x) {
  const int v[] = {5, 5, 3, 3, 3};
  return v[x];
}

template <int X> struct NTT {
  static constexpr int md = bmds(X);
  static constexpr int rt = brts(X);
  using M = ModInt<md>;
  vector<vector<M>> rts, rrts;

  void ensure_base(int n) {
    if ((int)rts.size() >= n)
      return;
    rts.resize(n);
    rrts.resize(n);
    for (int i = 1; i < n; i <<= 1) {
      if (!rts[i].empty())
        continue;
      M w = M(rt).power((md - 1) / (i << 1));
      M rw = w.inverse();
      rts[i].resize(i);
      rrts[i].resize(i);
      rts[i][0] = M(1);
      rrts[i][0] = M(1);
      for (int k = 1; k < i; k++) {
        rts[i][k] = rts[i][k - 1] * w;
        rrts[i][k] = rrts[i][k - 1] * rw;
      }
    }
  }

  void ntt(vector<M> &as, bool f) {
    int n = as.size();
    assert((n & (n - 1)) == 0);
    ensure_base(n);

    for (int i = 0, j = 1; j + 1 < n; j++) {
      for (int k = n >> 1; k > (i ^= k); k >>= 1)
        ;
      if (i > j)
        swap(as[i], as[j]);
    }

    for (int i = 1; i < n; i <<= 1) {
      for (int j = 0; j < n; j += i * 2) {
        for (int k = 0; k < i; k++) {
          M z = as[i + j + k] * (f ? rrts[i][k] : rts[i][k]);
          as[i + j + k] = as[j + k] - z;
          as[j + k] += z;
        }
      }
    }

    if (f) {
      M tmp = M(n).inverse();
      for (int i = 0; i < n; i++)
        as[i] *= tmp;
    }
  }

  vector<M> multiply(vector<M> as, vector<M> bs) {
    int need = as.size() + bs.size() - 1;
    int sz = 1;
    while (sz < need)
      sz <<= 1;
    as.resize(sz, M(0));
    bs.resize(sz, M(0));

    ntt(as, 0);
    ntt(bs, 0);
    for (int i = 0; i < sz; i++)
      as[i] *= bs[i];
    ntt(as, 1);

    as.resize(need);
    return as;
  }

  vector<int> multiply(vector<int> as, vector<int> bs) {
    vector<M> am(as.size()), bm(bs.size());
    for (int i = 0; i < (int)am.size(); i++)
      am[i] = M(as[i]);
    for (int i = 0; i < (int)bm.size(); i++)
      bm[i] = M(bs[i]);
    vector<M> cm = multiply(am, bm);
    vector<int> cs(cm.size());
    for (int i = 0; i < (int)cs.size(); i++)
      cs[i] = cm[i].x;
    return cs;
  }
};
template <int X> constexpr int NTT<X>::md;
template <int X> constexpr int NTT<X>::rt;

int M = 100000;
int n;
int h[100010];
int z[100010];
vector<vector<modint>> f;
ModFac MF(200010);
modint v[100010];
NTT<2> ntt;

vector<modint> mul(int l, int r) {
  if (r - l < 1)
    return vector<modint>(1, 1);
  if (r - l == 1)
    return f[l];
  int mid = (l + r) / 2;
  return ntt.multiply(mul(l, mid), mul(mid, r));
}

modint gr(int k, int j) {
  return MF.comb(k, 2 * j) * v[2 * j] * MF.inv_factorial(j);
}

void solve() {
  cin >> n;
  v[0] = 1;
  rep(i, n) { v[2 * i + 2] = v[2 * i] * MF.comb(2 * (i + 1), 2); }
  rep(i, 2 * n) {
    cin >> h[i];
    h[i]--;
    z[h[i]]++;
  }
  f.resize(M);
  rep(i, M) {
    f[i].resize(z[i] / 2 + 1);
    rep(j, z[i] / 2 + 1) { f[i][j] = gr(z[i], j); }
  }
  sort(f.begin(), f.end(), [](vector<modint> &a, vector<modint> &b) {
    return a.size() < b.size();
  });
  vector<modint> F = mul(0, M);
  F.resize(n + 1);
  vector<modint> G(n + 1);
  G[n] = F[n];
  per(i, n) {
    G[i] = F[i] * v[2 * n - 2 * i] * MF.comb(n, i) - (modint)(i + 1) * G[i + 1];
  }
  // rep(i,n+1){
  //     cout << i << " " << G[i] << " " << F[i] << endl;
  // }
  cout << G[0] * MF.inv_factorial(n) << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(50);
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define Per(i, sta, n) for (int i = n - 1; i >= sta; i--)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <int mod> struct ModInt {
  long long x;

  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  explicit operator int() const { return x; }

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return ModInt(u);
  }

  ModInt power(long long p) const {
    int a = x;
    if (p == 0)
      return 1;
    if (p == 1)
      return ModInt(a);
    if (p % 2 == 1)
      return (ModInt(a) * ModInt(a)).power(p / 2) * ModInt(a);
    else
      return (ModInt(a) * ModInt(a)).power(p / 2);
  }

  ModInt power(const ModInt p) const { return ((ModInt)x).power(p.x); }

  friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};

using modint = ModInt<mod>;

struct ModFac {
public:
  vector<modint> f, i_f;
  int n;

  ModFac(int n_) {
    n = n_;
    f.resize(n + 1, 1);
    i_f.resize(n + 1, 1);
    for (int i = 0; i < n; i++) {
      f[i + 1] = f[i] * (modint)(i + 1);
    }
    i_f[n] = f[n].power(mod - 2);
    for (int i = n - 1; i >= 0; i--) {
      i_f[i] = i_f[i + 1] * (modint)(i + 1);
    }
  }
  ModFac(modint n_) {
    n = (int)n_;
    f.resize(n + 1, 1);
    i_f.resize(n + 1, 1);
    for (int i = 0; i < n; i++) {
      f[i + 1] = f[i] * (modint)(i + 1);
    }
    i_f[n] = f[n].power(mod - 2);
    for (int i = n - 1; i >= 0; i--) {
      i_f[i] = i_f[i + 1] * (modint)(i + 1);
    }
  }

  modint factorial(int x) {
    // cout << f.size() << endl;
    return f[x];
  }

  modint inv_factorial(int x) { return i_f[x]; }

  modint comb(int m, int k) {
    if (m < 0 or k < 0)
      return 0;
    if (m < k)
      return 0;
    return f[m] * i_f[k] * i_f[m - k];
  }
};

constexpr int bmds(int x) {
  const int v[] = {1012924417, 924844033, 998244353, 897581057, 645922817};
  return v[x];
}
constexpr int brts(int x) {
  const int v[] = {5, 5, 3, 3, 3};
  return v[x];
}

template <int X> struct NTT {
  static constexpr int md = bmds(X);
  static constexpr int rt = brts(X);
  using M = ModInt<md>;
  vector<vector<M>> rts, rrts;

  void ensure_base(int n) {
    if ((int)rts.size() >= n)
      return;
    rts.resize(n);
    rrts.resize(n);
    for (int i = 1; i < n; i <<= 1) {
      if (!rts[i].empty())
        continue;
      M w = M(rt).power((md - 1) / (i << 1));
      M rw = w.inverse();
      rts[i].resize(i);
      rrts[i].resize(i);
      rts[i][0] = M(1);
      rrts[i][0] = M(1);
      for (int k = 1; k < i; k++) {
        rts[i][k] = rts[i][k - 1] * w;
        rrts[i][k] = rrts[i][k - 1] * rw;
      }
    }
  }

  void ntt(vector<M> &as, bool f) {
    int n = as.size();
    assert((n & (n - 1)) == 0);
    ensure_base(n);

    for (int i = 0, j = 1; j + 1 < n; j++) {
      for (int k = n >> 1; k > (i ^= k); k >>= 1)
        ;
      if (i > j)
        swap(as[i], as[j]);
    }

    for (int i = 1; i < n; i <<= 1) {
      for (int j = 0; j < n; j += i * 2) {
        for (int k = 0; k < i; k++) {
          M z = as[i + j + k] * (f ? rrts[i][k] : rts[i][k]);
          as[i + j + k] = as[j + k] - z;
          as[j + k] += z;
        }
      }
    }

    if (f) {
      M tmp = M(n).inverse();
      for (int i = 0; i < n; i++)
        as[i] *= tmp;
    }
  }

  vector<M> multiply(vector<M> as, vector<M> bs) {
    int need = as.size() + bs.size() - 1;
    int sz = 1;
    while (sz < need)
      sz <<= 1;
    as.resize(sz, M(0));
    bs.resize(sz, M(0));

    ntt(as, 0);
    ntt(bs, 0);
    for (int i = 0; i < sz; i++)
      as[i] *= bs[i];
    ntt(as, 1);

    as.resize(need);
    return as;
  }

  vector<int> multiply(vector<int> as, vector<int> bs) {
    vector<M> am(as.size()), bm(bs.size());
    for (int i = 0; i < (int)am.size(); i++)
      am[i] = M(as[i]);
    for (int i = 0; i < (int)bm.size(); i++)
      bm[i] = M(bs[i]);
    vector<M> cm = multiply(am, bm);
    vector<int> cs(cm.size());
    for (int i = 0; i < (int)cs.size(); i++)
      cs[i] = cm[i].x;
    return cs;
  }
};
template <int X> constexpr int NTT<X>::md;
template <int X> constexpr int NTT<X>::rt;

int M = 100000;
int n;
int h[100010];
int z[100010];
vector<vector<modint>> f;
ModFac MF(200010);
modint v[100010];

modint gr(int k, int j) {
  return MF.comb(k, 2 * j) * v[2 * j] * MF.inv_factorial(j);
}

void solve() {
  cin >> n;
  v[0] = 1;
  rep(i, n) { v[2 * i + 2] = v[2 * i] * MF.comb(2 * (i + 1), 2); }
  rep(i, 2 * n) {
    cin >> h[i];
    h[i]--;
    z[h[i]]++;
  }
  f.resize(M);
  rep(i, M) {
    f[i].resize(z[i] / 2 + 1);
    rep(j, z[i] / 2 + 1) { f[i][j] = gr(z[i], j); }
  }
  sort(f.begin(), f.end(), [](vector<modint> &a, vector<modint> &b) {
    return a.size() < b.size();
  });
  vector<modint> F = {1};
  NTT<2> ntt;
  rep(i, M) {
    if (f[i].size() == 1)
      continue;
    F = ntt.multiply(F, f[i]);
  }
  F.resize(n + 1);
  vector<modint> G(n + 1);
  G[n] = F[n];
  per(i, n) {
    G[i] = F[i] * v[2 * n - 2 * i] * MF.comb(n, i) - (modint)(i + 1) * G[i + 1];
  }
  // rep(i,n+1){
  //     cout << i << " " << G[i] << " " << F[i] << endl;
  // }
  cout << G[0] * MF.inv_factorial(n) << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(50);
  solve();
}