結果

問題 No.3785 Tree of Multiples
コンテスト
ユーザー apricity
提出日時 2026-11-02 17:44:27
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 557 ms / 6,000 ms
+ 394µs
コード長 22,832 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,296 ms
コンパイル使用メモリ 353,156 KB
実行使用メモリ 56,504 KB
最終ジャッジ日時 2026-11-02 17:44:39
合計ジャッジ時間 10,890 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifdef LOCAL
#include "template.hpp"
#else
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<utility>
#include<tuple>
#include<array>
#include<cstdint>
#include<cstdio>
#include<iomanip>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<stack>
#include<deque>
#include<bitset>
#include<cctype>
#include<chrono>
#include<random>
#include<cassert>
#include<cstddef>
#include<iterator>
#include<string_view>
#include<type_traits>
#include<functional>

using namespace std;

namespace io {

template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
    is >> p.first >> p.second;
    return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (auto &x : v) is >> x;
    return is;
}
template <typename T, size_t N = 0>
istream &operator>>(istream &is, array<T, N> &v) {
    for (auto &x : v) is >> x;
    return is;
}
template <size_t N = 0, typename T>
istream& cin_tuple_impl(istream &is, T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        is >> x;
        cin_tuple_impl<N + 1>(is, t);
    }
    return is;
}
template <class... T>
istream &operator>>(istream &is, tuple<T...> &t) {
    return cin_tuple_impl(is, t);
}

template<typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << p.first << " " << p.second;
    return os;
}
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template<typename T, size_t N>
ostream &operator<<(ostream &os, const array<T, N> &v) {
    size_t n = v.size();
    for (size_t i = 0; i < n; i++) {
        if (i) os << " ";
        os << v[i];
    }
    return os;
}
template <size_t N = 0, typename T>
ostream& cout_tuple_impl(ostream &os, const T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) os << " ";
        const auto &x = std::get<N>(t);
        os << x;
        cout_tuple_impl<N + 1>(os, t);
    }
    return os;
}
template <class... T>
ostream &operator<<(ostream &os, const tuple<T...> &t) {
    return cout_tuple_impl(os, t);
}

void in() {}
template<typename T, class... U>
void in(T &t, U &...u) {
    cin >> t;
    in(u...);
}
void out() { cout << "\n"; }
template<typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u) {
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}
void outr() {}
template<typename T, class... U, char sep = ' '>
void outr(const T &t, const U &...u) {
    cout << t;
    outr(u...);
}

void __attribute__((constructor)) _c() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
}
} // namespace io

using io::in;
using io::out;
using io::outr;

#define SHOW(x) static_cast<void>(0)

using ll = long long;
using D = double;
using LD = long double;
using P = pair<ll, ll>;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using vi = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template <class T> using vvvvvc = vector<vvvvc<T>>;
#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
template<typename T> using PQ = priority_queue<T,vector<T>>;
template<typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

#define rep1(a)          for(ll i = 0; i < a; i++)
#define rep2(i, a)       for(ll i = 0; i < a; i++)
#define rep3(i, a, b)    for(ll i = a; i < b; i++)
#define rep4(i, a, b, c) for(ll i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(a)          for(ll i = (a)-1; i >= 0; i--)
#define rrep2(i, a)       for(ll i = (a)-1; i >= 0; i--)
#define rrep3(i, a, b)    for(ll i = (b)-1; i >= a; i--)
#define rrep4(i, a, b, c) for(ll i = (b)-1; i >= a; i -= c)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define for_subset(t, s) for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )
#define SZ(v) ll(v.size())
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(c, x) distance((c).begin(), lower_bound(ALL(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(ALL(c), (x)))
template <typename T, typename U>
T SUM(const vector<U> &v) {
    T res = 0;
    for(auto &&a : v) res += a;
    return res;
}
template <typename T>
vector<pair<T,int>> RLE(const vector<T> &v) {
    if (v.empty()) return {};
    T cur = v.front();
    int cnt = 1;
    vector<pair<T,int>> res;
    for (int i = 1; i < (int)v.size(); i++) {
        if (cur == v[i]) cnt++;
        else {
            res.emplace_back(cur, cnt);
            cnt = 1; cur = v[i];
        }
    }
    res.emplace_back(cur, cnt);
    return res;
}
template<class T, class S>
inline bool chmax(T &a, const S &b) { return (a < b ? a = b, true : false); }
template<class T, class S>
inline bool chmin(T &a, const S &b) { return (a > b ? a = b, true : false); }
void YESNO(bool flag) { out(flag ? "YES" : "NO"); }
void yesno(bool flag) { out(flag ? "Yes" : "No"); }

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityl(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityl(x) & 1 ? -1 : 1); }
int highbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int highbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int highbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int highbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T get_bit(T x, int k) { return x >> k & 1; }
template <typename T>
T set_bit(T x, int k) { return x | T(1) << k; }
template <typename T>
T reset_bit(T x, int k) { return x & ~(T(1) << k); }
template <typename T>
T flip_bit(T x, int k) { return x ^ T(1) << k; }

template <typename T>
T popf(deque<T> &que) { T a = que.front(); que.pop_front(); return a; }
template <typename T>
T popb(deque<T> &que) { T a = que.back(); que.pop_back(); return a; }
template <typename T>
T pop(queue<T> &que) { T a = que.front(); que.pop(); return a; }
template <typename T>
T pop(stack<T> &que) { T a = que.top(); que.pop(); return a; }
template <typename T>
T pop(PQ<T> &que) { T a = que.top(); que.pop(); return a; }
template <typename T>
T pop(minPQ<T> &que) { T a = que.top(); que.pop(); return a; }

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
    if (check_ok) assert(check(ok));
    while (abs(ok -  ng) > 1) {
        ll mid = (ok + ng) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 60) {
    for (int _ = 0; _ < iter; _++) {
        double mid = (ok + ng) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    return (ok + ng) / 2;
}

// max x s.t. b*x <= a
ll div_floor(ll a, ll b) {
    assert(b != 0);
    if (b < 0) a = -a, b = -b;
    return a / b - (a % b < 0);
}
// max x s.t. b*x < a
ll div_under(ll a, ll b) {
    assert(b != 0);
    if (b < 0) a = -a, b = -b;
    return a / b - (a % b <= 0);
}
// min x s.t. b*x >= a
ll div_ceil(ll a, ll b) {
    assert(b != 0);
    if (b < 0) a = -a, b = -b;
    return a / b + (a % b > 0);
}
// min x s.t. b*x > a
ll div_over(ll a, ll b) {
    assert(b != 0);
    if (b < 0) a = -a, b = -b;
    return a / b + (a % b >= 0);
}
// x = a mod b (b > 0), 0 <= x < b
ll modulo(ll a, ll b) {
    assert(b > 0);
    ll c = a % b;
    return c < 0 ? c + b : c;
}
// (q,r) s.t. a = b*q + r, 0 <= r < b (b > 0)
// div_floor(a,b), modulo(a,b)
pair<ll,ll> divmod(ll a, ll b) {
    ll q = div_floor(a,b);
    return {q, a - b*q};
}
#endif

#include "atcoder/modint.hpp"
#include "atcoder/convolution.hpp"

template <typename mint>
struct Polynomial : vector<mint> {
    using vector<mint>::vector;
    using poly = Polynomial<mint>;

    void shrink() {
        while(!this->empty() and this->back() == mint(0)) this->pop_back();
    }

    poly rev() const {
        poly res(*this);
        reverse(res.begin(), res.end());
        return res;
    }

    poly pre(int sz) const {
        poly res(this->begin(), this->begin() + min(this->size(), (size_t)sz));
        if (res.size() < (size_t)sz) res.resize(sz);
        return res;
    }

    poly &operator>>=(int sz) {
        if (this->size() <= (size_t)sz) {
            this->clear();
            return *this;
        }
        this->erase(this->begin(), this->begin() + (size_t)sz);
        return *this;
    }

    poly &operator<<=(int sz) {
        this->insert(this->begin(), sz, mint::raw(0));
        return *this;
    }

    poly &operator+=(const mint &s) {
        if (this->empty()) this->resize(1);
        (*this)[0] += s;
        return *this;
    }

    poly &operator+=(const poly &p) {
        if (this->size() < p.size()) this->resize(p.size());
        for (size_t i = 0; i < p.size(); i++) (*this)[i] += p[i];
        return *this;
    }

    poly &operator-=(const mint &s) {
        if (this->empty()) this->resize(1);
        (*this)[0] -= s;
        return *this;
    }

    poly &operator-=(const poly &p) {
        if (this->size() < p.size()) this->resize(p.size());
        for (size_t i = 0; i < p.size(); i++) (*this)[i] -= p[i];
        return *this;
    }

    poly &operator*=(const mint &s) {
        for (size_t i = 0; i < this->size(); i++) (*this)[i] *= s;
        return *this;
    }

    poly operator*=(const poly &p) {
        if (this->empty() or p.empty()) {
            this->clear();
            return *this;
        }
        vector<mint> prod = atcoder::convolution(*this, p);
        return *this = poly(prod.begin(), prod.end());
    }

    poly &operator/=(const mint &s) {
        mint s_inv = s.inv();
        for (size_t i = 0; i < this->size(); i++) (*this)[i] *= s_inv;
        return *this;
    }

    poly operator/=(const poly &p) {
        if (this->size() < p.size()) {
            this->clear();
            return *this;
        }
        size_t q_sz = this->size() - p.size() + 1;
        return *this = (this->rev().pre(q_sz) * p.rev().inv(q_sz)).pre(q_sz).rev();
    }

    poly operator%=(const poly &p) {
        *this -= (*this / p) * p;
        shrink();
        assert(this->size() < p.size());
        return *this;
    }

    poly operator+(const mint &s) const { return poly(*this) += s; }
    poly operator+(const poly &p) const { return poly(*this) += p; }
    poly operator-(const mint &s) const { return poly(*this) -= s; }
    poly operator-(const poly &p) const { return poly(*this) -= p; }
    poly operator*(const mint &s) const { return poly(*this) *= s; }
    poly operator*(const poly &p) const { return poly(*this) *= p; }
    poly operator/(const mint &s) const { return poly(*this) /= s; }
    poly operator/(const poly &p) const { return poly(*this) /= p; }
    poly operator%(const poly &p) const { return poly(*this) %= p; }
    poly operator>>(int s) const { return poly(*this) >>= s; }
    poly operator<<(int s) const { return poly(*this) <<= s; }
    poly operator-() const {
        poly res(this->size());
        for (size_t i = 0; i < this->size(); i++) res[i] = -(*this)[i];
        return res;
    }

    poly dot(const poly &p) const {
        poly res(min(this->size(), p.size()));
        for (size_t i = 0; i < res.size(); i++) res[i] = (*this)[i] * p[i];
        return res;
    }

    mint eval(mint x) const {
        mint res = 0, pw = 1;
        for (auto &c : *this) res += c * pw, pw *= x;
        return res;
    }

    void ntt() {
        atcoder::internal::butterfly(*this);
    }

    void intt() {
        atcoder::internal::butterfly_inv(*this);
        mint sz_inv = mint::raw(this->size()).inv();
        for (auto &c : *this) c *= sz_inv;
    }

    poly inv(int deg = -1) const {
        assert(!this->empty() and (*this)[0] != 0);
        if (deg == -1) deg = (int)this->size();
        poly res(deg);
        res[0] = (*this)[0].inv();
        for (int d = 1; d < deg; d <<= 1) {
            poly f(2 * d), g(2 * d);
            for (int j = 0; j < min((int)this->size(), 2 * d); j++) f[j] = (*this)[j];
            for (int j = 0; j < d; j++) g[j] = res[j];
            // atcoder::internal::butterfly(f);
            f.ntt();
            // atcoder::internal::butterfly(g);
            g.ntt();
            for (int j = 0; j < 2 * d; j++) f[j] *= g[j];
            // atcoder::internal::butterfly_inv(f);
            f.intt();
            for (int j = 0; j < d; j++) f[j] = 0;
            // atcoder::internal::butterfly(f);
            f.ntt();
            for (int j = 0; j < 2 * d; j++) f[j] *= g[j];
            // atcoder::internal::butterfly_inv(f);
            f.intt();
            for (int j = d; j < min(2 * d, deg); j++) res[j] = -f[j];
        }
        return res.pre(deg);
    }

    poly differentiate() const {
        const size_t n = this->size();
        poly res(n == 0 ? 0 : n - 1);
        mint coef = 1;
        for (size_t i = 1; i < n; i++) {
            res[i - 1] = (*this)[i] * coef;
            coef += 1;
        }
        return res;
    }

    poly integrate() const {
        const size_t n = this->size();
        poly res(n + 1);
        res[0] = mint::raw(0);
        if (n > 0) res[1] = mint(1);
        int mod = mint::mod();
        for (size_t i = 2; i <= n; i++) res[i] = (-res[mod % i]) * (mod / i);
        for (size_t i = 0; i < n; i++) res[i + 1] *= (*this)[i];
        return res;
    }

    poly log(int deg = -1) const {
        assert(!this->empty() and (*this)[0] == 1);
        if (deg == -1) deg = (int)this->size();
        return (this->differentiate() * this->inv(deg)).pre(deg - 1).integrate();
    }

    poly exp(int deg = -1) const {
        assert(this->empty() or (*this)[0] == mint(0));
        if (deg == -1) deg = (int)this->size();

        vector<mint> inv;
        inv.reserve(deg + 1);
        inv.emplace_back(mint::raw(0));
        inv.emplace_back(mint::raw(1));
        poly b{1, 1 < (int)this->size() ? (*this)[1] : 0};
        poly c{1}, z1, z2{1, 1};
        int mod = mint::mod();
        for (int d = 2; d < deg ; d <<= 1) {
            poly y = b;
            y.resize(2 * d);
            // atcoder::internal::butterfly(y);
            y.ntt();
            z1 = z2;
            poly z(d);
            for (int i = 0; i < d; i++) z[i] = y[i] * z1[i];
            // atcoder::internal::butterfly_inv(z);
            z.intt();
            fill(z.begin(), z.begin() + d / 2, mint::raw(0));
            // atcoder::internal::butterfly(z);
            z.ntt();
            for (int i = 0; i < d; i++) z[i] *= -z1[i];
            // atcoder::internal::butterfly_inv(z);
            z.intt();
            c.insert(c.end(), z.begin() + d / 2, z.end());
            z2 = c;
            z2.resize(2 * d);
            // atcoder::internal::butterfly(z2);
            z2.ntt();
            poly x(this->begin(), this->begin() + min<int>(this->size(), d));
            x.resize(d);
            {
                x.erase(x.begin());
                mint coef = 1;
                for (int i = 0; i < d-1; i++) x[i] *= coef, coef += 1;
            }
            x.emplace_back(mint::raw(0));
            // atcoder::internal::butterfly(x);
            x.ntt();
            for (int i = 0; i < d; i++) x[i] *= y[i];
            // atcoder::internal::butterfly_inv(x);
            x.intt();
            x -= b.differentiate();
            x.resize(2 * d);
            for (int i = 0; i < d - 1; i++) x[d + i] = x[i], x[i] = mint::raw(0);
            // atcoder::internal::butterfly(x);
            x.ntt();
            for (int i = 0; i < 2 * d; i++) x[i] *= z2[i];
            // atcoder::internal::butterfly_inv(x);
            x.intt();
            x.pop_back();
            {
                int sz;
                while((sz = (int)inv.size()) <= (int)x.size()) {
                    inv.emplace_back((-inv[mod % sz]) * (mod / sz));
                }
                x.insert(x.begin(), mint::raw(0));
                for (size_t i = 1; i < x.size(); i++) x[i] *= inv[i];
            }
            for (int i = d; i < min((int)this->size(), 2 * d); i++) x[i] += (*this)[i];
            fill(x.begin(), x.begin() + d, mint::raw(0));
            // atcoder::internal::butterfly(x);
            x.ntt();
            for (int i = 0; i < 2 * d; i++) x[i] *= y[i];
            // atcoder::internal::butterfly_inv(x);
            x.intt();
            b.insert(b.end(), x.begin() + d, x.end());
        }
        return b.pre(deg);
    }

    poly pow(long long k, int deg = -1) const {
        const int n = (int)this->size();
        if (deg == -1) deg = n;
        if (k == 0) {
            poly res(deg);
            if (deg > 0) res[0] = 1;
            return res;
        }
        int l = 0;
        while (l < n and (*this)[l] == 0){
            l++;
            if (l >= (deg + k - 1) / k) return poly(deg, mint::raw(0));
        }
        if (l == n) return poly(deg, mint::raw(0));
        mint rev = (*this)[l].inv();
        poly res = (((*this * rev) >> l).log(deg) * k).exp(deg);
        res *= (*this)[l].pow(k);
        res = (res << (k * l)).pre(deg);
        if ((int)res.size() < deg) res.resize(deg, mint::raw(0));
        return res;
    }
};

using mint = atcoder::modint998244353;
using poly = Polynomial<mint>;

template<typename T> struct Binomial {
    vector<T> fact_, inv_, finv_;
    constexpr Binomial() {}
    constexpr Binomial(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
        init(n);
    }
    constexpr void init(int n) noexcept {
        constexpr int mod = T::mod();
        fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
        for(int i = 2; i < n; i++){
            fact_[i] = fact_[i-1] * i;
            inv_[i] = -inv_[mod%i] * (mod/i);
            finv_[i] = finv_[i-1] * inv_[i];
        }
    }
    constexpr T com(int n, int k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        return fact_[n] * finv_[k] * finv_[n-k];
    }
    constexpr T perm(int n, int k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        return fact_[n] * finv_[n-k];
    }
    constexpr T fact(int n) const noexcept {
        if (n < 0) return 0;
        return fact_[n];
    }
    constexpr T inv(int n) const noexcept {
        if (n < 0) return 0;
        return inv_[n];
    }
    constexpr T finv(int n) const noexcept {
        if (n < 0) return 0;
        return finv_[n];
    }
    constexpr T com_naive(int n, int k) const noexcept {
        if (n < 0 || k < 0 || n < k) return 0;
        T res = T(1);
        k = min(k, n-k);
        for (int i = 1; i <= k; i++)res *= (n--) * inv(i);
        return res;
    }
    template <typename I>
    constexpr T multi(const vector<I> &v) const noexcept {
        static_assert(is_integral<I>::value);
        I n = 0;
        for (auto& x : v) {
            if (x < 0) return 0;
            n += x;
        }
        T res = fact(n);
        for (auto &x : v) res *= finv(x);
        return res;
    }
    // [x^k] (1-x)^{-n} = com(n+k-1, k)
    constexpr T neg(int n, int k) const noexcept {
        if (n < 0 || k < 0) return 0;
        return k == 0 ? 1 : com(n+k-1, k);
    }
};
Binomial<mint> bc(300000);

void solve() {
    int n,m; in(n,m);
    map<int,int> mp1,mp2;
    vc<int> pq;
    int sa = 0, sb = 0;
    rep(i,n){
        int p,a; in(p,a);
        mp1[p] = a;
        sa += a;
        pq.emplace_back(p);
    }
    rep(i,m){
        int p,a; in(p,a);
        mp2[p] = a;
        sb += a;
        pq.emplace_back(p);
    }
    sort(ALL(pq)); UNIQUE(pq);
    mint ans = 0;
    {
        queue<poly> que;
        for(int pi : pq){
            int ai = mp1[pi];
            int bi = mp2[pi];
            int d = min(ai, bi);
            poly f(d+1);
            mint pw = 1;
            rep(k,d+1){
                f[k] = bc.finv(k)*bc.finv(ai-k)*bc.finv(bi-k)*pw;
                pw *= pi;
            }
            que.emplace(f);
        }
        while(SZ(que)>1){
            auto f1 = pop(que);
            auto f2 = pop(que);
            que.emplace(f1 * f2);
        }
        auto f = que.front();
        rep(i,SZ(f)) ans += bc.fact(i)*bc.fact(sa-i)*bc.fact(sb-i) * f[i];
    }
    {
        queue<poly> que1;
        queue<pair<poly,poly>> que2;
        for(int pi : pq){
            int ai = mp1[pi];
            int bi = mp2[pi];
            int d = min(ai, bi);
            poly f(d+1),g(d+1);
            mint pw = 1;
            rep(k,d+1){
                f[k] = bc.finv(k)*bc.finv(ai-k)*bc.finv(bi-k)*pw;
                g[k] = f[k] * (ai-k) * (bi-k);
                pw *= pi;
            }
            que1.emplace(f);
            que2.emplace(g,f);
        }
        while(SZ(que1)>1){
            auto f1 = pop(que1);
            auto f2 = pop(que1);
            que1.emplace(f1 * f2);
        }
        while(SZ(que2)>1){
            auto [num1,den1] = pop(que2);
            auto [num2,den2] = pop(que2);
            que2.emplace(num1*den2+num2*den1, den1*den2);
        }
        auto all1 = que1.front();
        auto [all2_num, all2_den] = que2.front();
        // poly all = all1 * all2_num * all2_den.inv();
        rep(i,SZ(all2_num)) ans -= bc.fact(i)*bc.fact(sa-1-i)*bc.fact(sb-1-i) * all2_num[i];
    }
    out(ans.val());
}

int main() {
    int tc = 1;
    // in(tc);
    while(tc--){
        solve();
    }
}
0