結果
問題 | No.235 めぐるはめぐる (5) |
ユーザー |
|
提出日時 | 2025-02-24 21:19:27 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,692 ms / 10,000 ms |
コード長 | 42,220 bytes |
コンパイル時間 | 5,202 ms |
コンパイル使用メモリ | 324,048 KB |
実行使用メモリ | 28,132 KB |
最終ジャッジ日時 | 2025-02-24 21:19:42 |
合計ジャッジ時間 | 13,323 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 3 |
ソースコード
// competitive-verifier: PROBLEM#include <iostream>#include <vector>/*** @brief 重み付きグラフ** @tparam T 辺の重みの型*/template <class T>struct Graph {private:struct _edge {constexpr _edge() : _from(), _to(), _weight() {}constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }constexpr int from() const { return _from; }constexpr int to() const { return _to; }constexpr T weight() const { return _weight; }private:int _from, _to;T _weight;};public:using edge_type = typename Graph<T>::_edge;Graph() : _size(), edges() {}Graph(int v) : _size(v), edges(v) {}const auto &operator[](int i) const { return edges[i]; }auto &operator[](int i) { return edges[i]; }const auto begin() const { return edges.begin(); }auto begin() { return edges.begin(); }const auto end() const { return edges.end(); }auto end() { return edges.end(); }constexpr int size() const { return _size; }void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }void add_edges(int from, int to, T weight = T(1)) {edges[from].emplace_back(from, to, weight);edges[to].emplace_back(to, from, weight);}void input_edge(int m, int base = 1) {for (int i = 0; i < m; ++i) {int from, to;T weight;std::cin >> from >> to >> weight;add_edge(from - base, to - base, weight);}}void input_edges(int m, int base = 1) {for (int i = 0; i < m; ++i) {int from, to;T weight;std::cin >> from >> to >> weight;add_edges(from - base, to - base, weight);}}private:int _size;std::vector<std::vector<edge_type>> edges;};template <>struct Graph<void> {private:struct _edge {constexpr _edge() : _from(), _to() {}constexpr _edge(int from, int to) : _from(from), _to(to) {}constexpr int from() const { return _from; }constexpr int to() const { return _to; }constexpr int weight() const { return 1; }constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }private:int _from, _to;};public:using edge_type = typename Graph<void>::_edge;Graph() : _size(), edges() {}Graph(int v) : _size(v), edges(v) {}const auto &operator[](int i) const { return edges[i]; }auto &operator[](int i) { return edges[i]; }const auto begin() const { return edges.begin(); }auto begin() { return edges.begin(); }const auto end() const { return edges.end(); }auto end() { return edges.end(); }constexpr int size() const { return _size; }void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }void add_edge(int from, int to) { edges[from].emplace_back(from, to); }void add_edges(int from, int to) {edges[from].emplace_back(from, to);edges[to].emplace_back(to, from);}void input_edge(int m, int base = 1) {for (int i = 0; i < m; ++i) {int from, to;std::cin >> from >> to;add_edge(from - base, to - base);}}void input_edges(int m, int base = 1) {for (int i = 0; i < m; ++i) {int from, to;std::cin >> from >> to;add_edges(from - base, to - base);}}private:int _size;std::vector<std::vector<edge_type>> edges;};#include <cstdint>#include <type_traits>#include <utility>namespace internal {// @param m `1 <= m`// @return x mod mconstexpr std::int64_t safe_mod(std::int64_t x, std::int64_t m) {x %= m;if (x < 0) x += m;return x;}// Fast modular multiplication by barrett reduction// Reference: https://en.wikipedia.org/wiki/Barrett_reduction// NOTE: reconsider after Ice Lakestruct barrett {unsigned int _m;std::uint64_t im;// @param m `1 <= m`explicit barrett(unsigned int m) : _m(m), im((std::uint64_t)(-1) / m + 1) {}// @return munsigned int umod() const { return _m; }// @param a `0 <= a < m`// @param b `0 <= b < m`// @return `a * b % m`unsigned int mul(unsigned int a, unsigned int b) const {std::uint64_t z = a;z *= b;std::uint64_t x = (std::uint64_t)(((__uint128_t)(z)*im) >> 64);std::uint64_t y = x * _m;return (unsigned int)(z - y + (z < y ? _m : 0));}};struct montgomery {std::uint64_t _m;std::uint64_t im;std::uint64_t r2;// @param m `1 <= m`explicit constexpr montgomery(std::uint64_t m) : _m(m), im(m), r2(-__uint128_t(m) % m) {for (int i = 0; i < 5; ++i) im = im * (2 - _m * im);im = -im;}// @return mconstexpr std::uint64_t umod() const { return _m; }// @param a `0 <= a < m`// @param b `0 <= b < m`// @return `a * b % m`constexpr std::uint64_t mul(std::uint64_t a, std::uint64_t b) const { return mr(mr(a, b), r2); }constexpr std::uint64_t exp(std::uint64_t a, std::uint64_t b) const {std::uint64_t res = 1, p = mr(a, r2);while (b) {if (b & 1) res = mr(res, p);p = mr(p, p);b >>= 1;}return res;}constexpr bool same_pow(std::uint64_t x, int s, std::uint64_t n) const {x = mr(x, r2), n = mr(n, r2);for (int r = 0; r < s; r++) {if (x == n) return true;x = mr(x, x);}return false;}private:constexpr std::uint64_t mr(std::uint64_t x) const {return ((__uint128_t)(x * im) * _m + x) >> 64;}constexpr std::uint64_t mr(std::uint64_t a, std::uint64_t b) const {__uint128_t t = (__uint128_t)a * b;std::uint64_t inc = std::uint64_t(t) != 0;std::uint64_t x = t >> 64, y = ((__uint128_t)(a * b * im) * _m) >> 64;unsigned long long z = 0;bool f = __builtin_uaddll_overflow(x, y, &z);z += inc;return f ? z - _m : z;}};constexpr bool is_SPRP32(std::uint32_t n, std::uint32_t a) {std::uint32_t d = n - 1, s = 0;while ((d & 1) == 0) ++s, d >>= 1;std::uint64_t cur = 1, pw = d;while (pw) {if (pw & 1) cur = (cur * a) % n;a = (std::uint64_t)a * a % n;pw >>= 1;}if (cur == 1) return true;for (std::uint32_t r = 0; r < s; r++) {if (cur == n - 1) return true;cur = cur * cur % n;}return false;}// given 2 <= n,a < 2^64, a prime, check whether n is a-SPRPconstexpr bool is_SPRP64(const montgomery &m, std::uint64_t a) {auto n = m.umod();if (n == a) return true;if (n % a == 0) return false;std::uint64_t d = n - 1;int s = 0;while ((d & 1) == 0) ++s, d >>= 1;std::uint64_t cur = m.exp(a, d);if (cur == 1) return true;return m.same_pow(cur, s, n - 1);}constexpr bool is_prime_constexpr(std::uint64_t x) {if (x == 2 || x == 3 || x == 5 || x == 7) return true;if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0 || x % 7 == 0) return false;if (x < 121) return (x > 1);montgomery m(x);constexpr std::uint64_t bases[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};for (auto a : bases) {if (!is_SPRP64(m, a)) return false;}return true;}constexpr bool is_prime_constexpr(std::int64_t x) {if (x < 0) return false;return is_prime_constexpr(std::uint64_t(x));}constexpr bool is_prime_constexpr(std::uint32_t x) {if (x == 2 || x == 3 || x == 5 || x == 7) return true;if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0 || x % 7 == 0) return false;if (x < 121) return (x > 1);std::uint64_t h = x;h = ((h >> 16) ^ h) * 0x45d9f3b;h = ((h >> 16) ^ h) * 0x45d9f3b;h = ((h >> 16) ^ h) & 255;constexpr uint16_t bases[] = {15591, 2018, 166, 7429, 8064, 16045, 10503, 4399, 1949, 1295, 2776, 3620, 560,3128, 5212, 2657, 2300, 2021, 4652, 1471, 9336, 4018, 2398, 20462, 10277, 8028,2213, 6219, 620, 3763, 4852, 5012, 3185, 1333, 6227, 5298, 1074, 2391, 5113,7061, 803, 1269, 3875, 422, 751, 580, 4729, 10239, 746, 2951, 556, 2206,3778, 481, 1522, 3476, 481, 2487, 3266, 5633, 488, 3373, 6441, 3344, 17,15105, 1490, 4154, 2036, 1882, 1813, 467, 3307, 14042, 6371, 658, 1005, 903,737, 1887, 7447, 1888, 2848, 1784, 7559, 3400, 951, 13969, 4304, 177, 41,19875, 3110, 13221, 8726, 571, 7043, 6943, 1199, 352, 6435, 165, 1169, 3315,978, 233, 3003, 2562, 2994, 10587, 10030, 2377, 1902, 5354, 4447, 1555, 263,27027, 2283, 305, 669, 1912, 601, 6186, 429, 1930, 14873, 1784, 1661, 524,3577, 236, 2360, 6146, 2850, 55637, 1753, 4178, 8466, 222, 2579, 2743, 2031,2226, 2276, 374, 2132, 813, 23788, 1610, 4422, 5159, 1725, 3597, 3366, 14336,579, 165, 1375, 10018, 12616, 9816, 1371, 536, 1867, 10864, 857, 2206, 5788,434, 8085, 17618, 727, 3639, 1595, 4944, 2129, 2029, 8195, 8344, 6232, 9183,8126, 1870, 3296, 7455, 8947, 25017, 541, 19115, 368, 566, 5674, 411, 522,1027, 8215, 2050, 6544, 10049, 614, 774, 2333, 3007, 35201, 4706, 1152, 1785,1028, 1540, 3743, 493, 4474, 2521, 26845, 8354, 864, 18915, 5465, 2447, 42,4511, 1660, 166, 1249, 6259, 2553, 304, 272, 7286, 73, 6554, 899, 2816,5197, 13330, 7054, 2818, 3199, 811, 922, 350, 7514, 4452, 3449, 2663, 4708,418, 1621, 1171, 3471, 88, 11345, 412, 1559, 194};return is_SPRP32(x, bases[h]);}// @param n `0 <= n`// @param m `1 <= m`// @return `(x ** n) % m`constexpr std::int64_t pow_mod_constexpr(std::int64_t x, std::int64_t n, int m) {if (m == 1) return 0;unsigned int _m = (unsigned int)(m);std::uint64_t r = 1;std::uint64_t y = safe_mod(x, m);while (n) {if (n & 1) r = (r * y) % _m;y = (y * y) % _m;n >>= 1;}return r;}// Reference:// M. Forisek and J. Jancina,// Fast Primality Testing for Integers That Fit into a Machine Word// @param n `0 <= n`constexpr bool is_prime_constexpr(int n) {if (n <= 1) return false;if (n == 2 || n == 7 || n == 61) return true;if (n % 2 == 0) return false;std::int64_t d = n - 1;while (d % 2 == 0) d /= 2;constexpr std::int64_t bases[3] = {2, 7, 61};for (std::int64_t a : bases) {std::int64_t t = d;std::int64_t y = pow_mod_constexpr(a, t, n);while (t != n - 1 && y != 1 && y != n - 1) {y = y * y % n;t <<= 1;}if (y != n - 1 && t % 2 == 0) { return false; }}return true;}template <int n>constexpr bool is_prime = is_prime_constexpr(n);// @param b `1 <= b`// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/gconstexpr std::pair<std::int64_t, std::int64_t> inv_gcd(std::int64_t a, std::int64_t b) {a = safe_mod(a, b);if (a == 0) return {b, 0};std::int64_t s = b, t = a;std::int64_t m0 = 0, m1 = 1;while (t) {std::int64_t u = s / t;s -= t * u;m0 -= m1 * u;auto tmp = s;s = t;t = tmp;tmp = m0;m0 = m1;m1 = tmp;}if (m0 < 0) m0 += b / s;return {s, m0};}// Compile time primitive root// @param m must be prime// @return primitive root (and minimum in now)constexpr int primitive_root_constexpr(int m) {if (m == 2) return 1;if (m == 167772161) return 3;if (m == 469762049) return 3;if (m == 754974721) return 11;if (m == 998244353) return 3;int divs[20] = {};divs[0] = 2;int cnt = 1;int x = (m - 1) / 2;while (x % 2 == 0) x /= 2;for (int i = 3; (std::int64_t)(i)*i <= x; i += 2) {if (x % i == 0) {divs[cnt++] = i;while (x % i == 0) { x /= i; }}}if (x > 1) { divs[cnt++] = x; }for (int g = 2;; g++) {bool ok = true;for (int i = 0; i < cnt; i++) {if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {ok = false;break;}}if (ok) return g;}}template <int m>constexpr int primitive_root = primitive_root_constexpr(m);} // namespace internal#include <cassert>#include <numeric>namespace internal {template <class T>using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value ||std::is_same<T, __int128>::value,std::true_type, std::false_type>::type;template <class T>using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value ||std::is_same<T, unsigned __int128>::value,std::true_type, std::false_type>::type;template <class T>using make_unsigned_int128 =typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>;template <class T>using is_integral =typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value ||is_unsigned_int128<T>::value,std::true_type, std::false_type>::type;template <class T>using is_signed_int =typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) ||is_signed_int128<T>::value,std::true_type, std::false_type>::type;template <class T>using is_unsigned_int =typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) ||is_unsigned_int128<T>::value,std::true_type, std::false_type>::type;template <class T>using to_unsigned = typename std::conditional<is_signed_int128<T>::value, make_unsigned_int128<T>,typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>,std::common_type<T>>::type>::type;template <class T>using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;template <class T>using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;template <class T>using to_unsigned_t = typename to_unsigned<T>::type;} // namespace internalnamespace internal {struct modint_base {};struct static_modint_base : modint_base {};template <class T>using is_modint = std::is_base_of<modint_base, T>;template <class T>using is_modint_t = std::enable_if_t<is_modint<T>::value>;} // namespace internaltemplate <int m, std::enable_if_t<(1 <= m)> * = nullptr>struct static_modint : internal::static_modint_base {using mint = static_modint;public:static constexpr int mod() { return m; }static constexpr mint raw(int v) {mint x;x._v = v;return x;}constexpr static_modint() : _v(0) {}template <class T, internal::is_signed_int_t<T> * = nullptr>constexpr static_modint(T v) : _v(0) {std::int64_t x = (std::int64_t)(v % (std::int64_t)(umod()));if (x < 0) x += umod();_v = (unsigned int)(x);}template <class T, internal::is_unsigned_int_t<T> * = nullptr>constexpr static_modint(T v) : _v(0) {_v = (unsigned int)(v % umod());}constexpr unsigned int val() const { return _v; }constexpr mint &operator++() {_v++;if (_v == umod()) _v = 0;return *this;}constexpr mint &operator--() {if (_v == 0) _v = umod();_v--;return *this;}constexpr mint operator++(int) {mint result = *this;++*this;return result;}constexpr mint operator--(int) {mint result = *this;--*this;return result;}constexpr mint &operator+=(const mint &rhs) {_v += rhs._v;if (_v >= umod()) _v -= umod();return *this;}constexpr mint &operator-=(const mint &rhs) {_v -= rhs._v;if (_v >= umod()) _v += umod();return *this;}constexpr mint &operator*=(const mint &rhs) {std::uint64_t z = _v;z *= rhs._v;_v = (unsigned int)(z % umod());return *this;}constexpr mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); }constexpr mint operator+() const { return *this; }constexpr mint operator-() const { return mint() - *this; }constexpr mint pow(std::int64_t n) const {assert(0 <= n);mint x = *this, r = 1;while (n) {if (n & 1) r *= x;x *= x;n >>= 1;}return r;}constexpr mint inv() const {if (prime) {assert(_v);return pow(umod() - 2);} else {auto eg = internal::inv_gcd(_v, m);assert(eg.first == 1);return eg.second;}}friend constexpr mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; }friend constexpr mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; }friend constexpr mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; }friend constexpr mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; }friend constexpr bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; }friend constexpr bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; }friend std::istream &operator>>(std::istream &is, mint &rhs) {std::int64_t t;is >> t;rhs = mint(t);return is;}friend constexpr std::ostream &operator<<(std::ostream &os, const mint &rhs) {return os << rhs._v;}private:unsigned int _v;static constexpr unsigned int umod() { return m; }static constexpr bool prime = internal::is_prime<m>;};template <int id>struct dynamic_modint : internal::modint_base {using mint = dynamic_modint;public:static int mod() { return (int)(bt.umod()); }static void set_mod(int m) {assert(1 <= m);bt = internal::barrett(m);}static mint raw(int v) {mint x;x._v = v;return x;}dynamic_modint() : _v(0) {}template <class T, internal::is_signed_int_t<T> * = nullptr>dynamic_modint(T v) {std::int64_t x = (std::int64_t)(v % (std::int64_t)(mod()));if (x < 0) x += mod();_v = (unsigned int)(x);}template <class T, internal::is_unsigned_int_t<T> * = nullptr>dynamic_modint(T v) {_v = (unsigned int)(v % mod());}unsigned int val() const { return _v; }mint &operator++() {_v++;if (_v == umod()) _v = 0;return *this;}mint &operator--() {if (_v == 0) _v = umod();_v--;return *this;}mint operator++(int) {mint result = *this;++*this;return result;}mint operator--(int) {mint result = *this;--*this;return result;}mint &operator+=(const mint &rhs) {_v += rhs._v;if (_v >= umod()) _v -= umod();return *this;}mint &operator-=(const mint &rhs) {_v += mod() - rhs._v;if (_v >= umod()) _v -= umod();return *this;}mint &operator*=(const mint &rhs) {_v = bt.mul(_v, rhs._v);return *this;}mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); }mint operator+() const { return *this; }mint operator-() const { return mint() - *this; }mint pow(std::int64_t n) const {assert(0 <= n);mint x = *this, r = 1;while (n) {if (n & 1) r *= x;x *= x;n >>= 1;}return r;}mint inv() const {auto eg = internal::inv_gcd(_v, mod());assert(eg.first == 1);return eg.second;}friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; }friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; }friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; }friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; }friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; }friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; }friend std::istream &operator>>(std::istream &is, mint &rhs) {std::int64_t t;is >> t;rhs = mint(t);return is;}friend constexpr std::ostream &operator<<(std::ostream &os, const mint &rhs) {return os << rhs._v;}private:unsigned int _v;static internal::barrett bt;static unsigned int umod() { return bt.umod(); }};template <int id>internal::barrett dynamic_modint<id>::bt(998244353);using modint998 = static_modint<998244353>;using modint107 = static_modint<1000000007>;using modint = dynamic_modint<-1>;namespace internal {template <class T>using is_static_modint = std::is_base_of<internal::static_modint_base, T>;template <class T>using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;template <class>struct is_dynamic_modint : public std::false_type {};template <int id>struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};template <class T>using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;} // namespace internalnamespace internal {// @return same with std::bit::bit_ceilunsigned int bit_ceil(unsigned int n) {unsigned int x = 1;while (x < (unsigned int)(n)) x *= 2;return x;}// @param n `1 <= n`// @return same with std::bit::countl_zeroint countl_zero(unsigned int n) { return __builtin_clz(n); }// @param n `1 <= n`// @return same with std::bit::countr_zeroint countr_zero(unsigned int n) { return __builtin_ctz(n); }// @param n `1 <= n`// @return same with std::bit::countr_zeroconstexpr int countr_zero_constexpr(unsigned int n) {int x = 0;while (!(n & (1 << x))) x++;return x;}} // namespace internal#include <algorithm>#include <limits>template <class T>struct Add {using value_type = T;static constexpr T id() { return T(); }static constexpr T op(const T &lhs, const T &rhs) { return lhs + rhs; }template <class U>static constexpr U f(T lhs, U rhs) {return lhs + rhs;}};template <class T>struct Mul {using value_type = T;static constexpr T id() { return T(1); }static constexpr T op(const T &lhs, const T &rhs) { return lhs * rhs; }template <class U>static constexpr U f(T lhs, U rhs) {return lhs * rhs;}};template <class T>struct And {using value_type = T;static constexpr T id() { return std::numeric_limits<T>::max(); }static constexpr T op(const T &lhs, const T &rhs) { return lhs & rhs; }template <class U>static constexpr U f(T lhs, U rhs) {return lhs & rhs;}};template <class T>struct Or {using value_type = T;static constexpr T id() { return T(); }static constexpr T op(const T &lhs, const T &rhs) { return lhs | rhs; }template <class U>static constexpr U f(T lhs, U rhs) {return lhs | rhs;}};template <class T>struct Xor {using value_type = T;static constexpr T id() { return T(); }static constexpr T op(const T &lhs, const T &rhs) { return lhs ^ rhs; }template <class U>static constexpr U f(T lhs, U rhs) {return lhs ^ rhs;}};template <class T>struct Min {using value_type = T;static constexpr T id() { return std::numeric_limits<T>::max(); }static constexpr T op(const T &lhs, const T &rhs) { return std::min(lhs, rhs); }template <class U>static constexpr U f(T lhs, U rhs) {return std::min((U)lhs, rhs);}};template <class T>struct Max {using value_type = T;static constexpr T id() { return std::numeric_limits<T>::lowest(); }static constexpr T op(const T &lhs, const T &rhs) { return std::max(lhs, rhs); }template <class U>static constexpr U f(T lhs, U rhs) {return std::max((U)lhs, rhs);}};template <class T>struct Gcd {using value_type = T;static constexpr T id() { return std::numeric_limits<T>::max(); }static constexpr T op(const T &lhs, const T &rhs) {return lhs == Gcd::id() ? rhs : (rhs == Gcd::id() ? lhs : std::gcd(lhs, rhs));}};template <class T>struct Lcm {using value_type = T;static constexpr T id() { return std::numeric_limits<T>::max(); }static constexpr T op(const T &lhs, const T &rhs) {return lhs == Lcm::id() ? rhs : (rhs == Lcm::id() ? lhs : std::lcm(lhs, rhs));}};template <class T>struct Update {using value_type = T;static constexpr T id() { return std::numeric_limits<T>::max(); }static constexpr T op(const T &lhs, const T &rhs) { return lhs == Update::id() ? rhs : lhs; }template <class U>static constexpr U f(T lhs, U rhs) {return lhs == Update::id() ? rhs : lhs;}};template <class T>struct Affine {using P = std::pair<T, T>;using value_type = P;static constexpr P id() { return P(1, 0); }static constexpr P op(P lhs, P rhs) {return {lhs.first * rhs.first, lhs.first * rhs.second + lhs.second};}};template <class M>struct Rev {using T = typename M::value_type;using value_type = T;static constexpr T id() { return M::id(); }static constexpr T op(T lhs, T rhs) { return M::op(rhs, lhs); }};/*** @brief 遅延評価セグメント木* @see https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp** @tparam S モノイド* @tparam F モノイド*/template <class S, class F>struct lazy_segment_tree {private:using T = typename S::value_type;using U = typename F::value_type;public:lazy_segment_tree() : lazy_segment_tree(0) {}explicit lazy_segment_tree(int n, T e = S::id()) : lazy_segment_tree(std::vector<T>(n, e)) {}explicit lazy_segment_tree(const std::vector<T> &v) : _n(int(v.size())) {_size = internal::bit_ceil(_n);_log = internal::countr_zero(_size);data = std::vector<T>(2 * _size, S::id());lazy = std::vector<U>(_size, F::id());for (int i = 0; i < _n; i++) data[_size + i] = v[i];for (int i = _size - 1; i >= 1; --i) update(i);}void set(int p, T x) {assert(0 <= p && p < _n);p += _size;for (int i = _log; i >= 1; --i) push(p >> i);data[p] = x;for (int i = 1; i <= _log; ++i) update(p >> i);}T at(int p) { return get(p); }T get(int p) {assert(0 <= p && p < _n);p += _size;for (int i = _log; i >= 1; --i) push(p >> i);return data[p];}void apply(int p, U f) {assert(0 <= p && p < _n);p += _size;for (int i = _log; i >= 1; --i) push(p >> i);data[p] = F::f(f, data[p]);for (int i = 1; i <= _log; ++i) update(p >> i);}void apply(int l, int r, U f) {assert(0 <= l && l <= r && r <= _n);if (l == r) return;l += _size, r += _size;for (int i = _log; i >= 1; --i) {if (((l >> i) << i) != l) push(l >> i);if (((r >> i) << i) != r) push((r - 1) >> i);}int l2 = l, r2 = r;while (l < r) {if (l & 1) all_apply(l++, f);if (r & 1) all_apply(--r, f);l >>= 1, r >>= 1;}l = l2, r = r2;for (int i = 1; i <= _log; i++) {if (((l >> i) << i) != l) update(l >> i);if (((r >> i) << i) != r) update((r - 1) >> i);}}T prod(int l, int r) {assert(0 <= l && l <= r && r <= _n);if (l == r) return S::id();l += _size, r += _size;for (int i = _log; i >= 1; --i) {if (((l >> i) << i) != l) push(l >> i);if (((r >> i) << i) != r) push((r - 1) >> i);}T sml = S::id(), smr = S::id();while (l < r) {if (l & 1) sml = S::op(sml, data[l++]);if (r & 1) smr = S::op(data[--r], smr);l >>= 1, r >>= 1;}return S::op(sml, smr);}T all_prod() const { return data[1]; }int max_right(int l, T val) {auto f = [&val](T v) { return !(val < v); };return max_right(l, f);}template <class G>int max_right(int l, G g) {assert(0 <= l && l <= _n);assert(g(S::id()));if (l == _n) return _n;l += _size;for (int i = _log; i >= 1; i--) push(l >> i);T sm = S::id();do {while (l % 2 == 0) l >>= 1;if (!g(S::op(sm, data[l]))) {while (l < _size) {push(l);l = (2 * l);if (g(S::op(sm, data[l]))) {sm = S::op(sm, data[l]);l++;}}return l - _size;}sm = S::op(sm, data[l]);l++;} while ((l & -l) != l);return _n;}int min_left(int l, T val) {auto f = [&val](T v) { return !(val < v); };return min_left(l, f);}template <class G>int min_left(int r, G g) {assert(0 <= r && r <= _n);assert(g(S::id()));if (r == 0) return 0;r += _size;for (int i = _log; i >= 1; i--) push((r - 1) >> i);S sm = S::id();do {r--;while (r > 1 && (r % 2)) r >>= 1;if (!g(S::op(data[r], sm))) {while (r < _size) {push(r);r = (2 * r + 1);if (g(S::op(data[r], sm))) {sm = S::op(data[r], sm);r--;}}return r + 1 - _size;}sm = S::op(data[r], sm);} while ((r & -r) != r);return 0;}private:int _n, _size, _log;std::vector<T> data;std::vector<U> lazy;void update(int k) { data[k] = S::op(data[2 * k], data[2 * k + 1]); }void all_apply(int k, U f) {data[k] = F::f(f, data[k]);if (k < _size) lazy[k] = F::op(f, lazy[k]);}void push(int k) {all_apply(2 * k, lazy[k]);all_apply(2 * k + 1, lazy[k]);lazy[k] = F::id();}};#ifdef ATCODER#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")#endif#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")#include <bits/stdc++.h>#ifndef ATCODER#pragma GCC target("sse4.2,avx2,bmi2")#endiftemplate <class T, class U>constexpr bool chmax(T &a, const U &b) {return a < (T)b ? a = (T)b, true : false;}template <class T, class U>constexpr bool chmin(T &a, const U &b) {return (T)b < a ? a = (T)b, true : false;}constexpr std::int64_t INF = 1000000000000000003;constexpr int Inf = 1000000003;constexpr double EPS = 1e-7;constexpr double PI = 3.14159265358979323846;#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)#define rep(i, n) FOR (i, 0, n)#define repn(i, n) FOR (i, 1, n + 1)#define repr(i, n) FORR (i, n, 0)#define repnr(i, n) FORR (i, n + 1, 1)#define all(s) (s).begin(), (s).end()struct Sonic {Sonic() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout << std::fixed << std::setprecision(20);}constexpr void operator()() const {}} sonic;using namespace std;using ll = std::int64_t;using ld = long double;template <class T, class U>std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {return is >> p.first >> p.second;}template <class T>std::istream &operator>>(std::istream &is, std::vector<T> &v) {for (T &i : v) is >> i;return is;}template <class T, class U>std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {return os << '(' << p.first << ',' << p.second << ')';}template <class T>std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? "" : " ") << *it;return os;}template <class Head, class... Tail>void co(Head &&head, Tail &&...tail) {if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';else std::cout << head << ' ', co(std::forward<Tail>(tail)...);}template <class Head, class... Tail>void ce(Head &&head, Tail &&...tail) {if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);}void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes\n" : "No\n"); }void No(bool is_not_correct = true) { Yes(!is_not_correct); }void YES(bool is_correct = true) { std::cout << (is_correct ? "YES\n" : "NO\n"); }void NO(bool is_not_correct = true) { YES(!is_not_correct); }void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; }void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }namespace internal {struct graph_csr {private:struct edge_list {using const_iterator = std::vector<int>::const_iterator;edge_list(const graph_csr &g, int v) : g(g), v(v) {}const_iterator begin() const { return std::next(g.elist.begin(), g.start[v]); }const_iterator end() const { return std::next(g.elist.begin(), g.start[v + 1]); }private:const graph_csr &g;int v;};public:graph_csr(int n) : _size(n), edges(), start(n + 1) {}edge_list operator[](int i) const { return edge_list(*this, i); }constexpr int size() const { return _size; }void build() {for (auto [u, v] : edges) ++start[u + 1];for (int i = 0; i < _size; ++i) start[i + 1] += start[i];auto counter = start;elist = std::vector<int>(edges.size());for (auto [u, v] : edges) elist[counter[u]++] = v;}void add_edge(int u, int v) { edges.emplace_back(u, v); }void add_edges(int u, int v) {edges.emplace_back(u, v);edges.emplace_back(v, u);}void input_edge(int m, int base = 1) {for (int i = 0; i < m; ++i) {int from, to;std::cin >> from >> to;add_edge(from - base, to - base);}build();}void input_edges(int m, int base = 1) {for (int i = 0; i < m; ++i) {int from, to;std::cin >> from >> to;add_edges(from - base, to - base);}build();}int _size;std::vector<std::pair<int, int>> edges;std::vector<int> elist;std::vector<int> start;};} // namespace internal/*** @brief HL分解* @see https://beet-aizu.github.io/library/tree/heavylightdecomposition.cpp*/struct heavy_light_decomposition {heavy_light_decomposition() = default;template <class T>heavy_light_decomposition(const Graph<T> &g, int r = 0) : heavy_light_decomposition(g.size()) {std::vector<int> heavy_path(_size, -1), sub_size(_size, 1);std::stack<int> st;st.emplace(r);int pos = 0;while (!st.empty()) {int v = st.top();st.pop();vid[pos++] = v;for (auto &e : g[v]) {int u = e.to();if (u == par[v]) continue;par[u] = v, dep[u] = dep[v] + 1, st.emplace(u);}}for (int i = _size - 1; i >= 0; --i) {int v = vid[i];int max_sub = 0;for (auto &e : g[v]) {int u = e.to();if (u == par[v]) continue;sub_size[v] += sub_size[u];if (max_sub < sub_size[u]) max_sub = sub_size[u], heavy_path[v] = u;}}nxt[r] = r;pos = 0;st.emplace(r);while (!st.empty()) {int v = st.top();st.pop();vid[v] = pos++;inv[vid[v]] = v;int hp = heavy_path[v];for (auto &e : g[v]) {int u = e.to();if (u == par[v] || u == hp) continue;nxt[u] = u, st.emplace(u);}if (hp != -1) nxt[hp] = nxt[v], st.emplace(hp);}}heavy_light_decomposition(const internal::graph_csr &g, int r = 0): heavy_light_decomposition(g.size()) {std::vector<int> heavy_path(_size, -1), sub_size(_size, 1);std::stack<int> st;st.emplace(r);int pos = 0;while (!st.empty()) {int v = st.top();st.pop();vid[pos++] = v;for (int u : g[v]) {if (u == par[v]) continue;par[u] = v, dep[u] = dep[v] + 1, st.emplace(u);}}for (int i = _size - 1; i >= 0; --i) {int v = vid[i];int max_sub = 0;for (int u : g[v]) {if (u == par[v]) continue;sub_size[v] += sub_size[u];if (max_sub < sub_size[u]) max_sub = sub_size[u], heavy_path[v] = u;}}nxt[r] = r;pos = 0;st.emplace(r);while (!st.empty()) {int v = st.top();st.pop();vid[v] = pos++;inv[vid[v]] = v;int hp = heavy_path[v];for (int u : g[v]) {if (u == par[v] || u == hp) continue;nxt[u] = u, st.emplace(u);}if (hp != -1) nxt[hp] = nxt[v], st.emplace(hp);}}constexpr int size() const { return _size; }int get(int v) const { return vid[v]; }int get_parent(int v) const { return par[v]; }int get_depth(int v) const { return dep[v]; }int dist(int u, int v) const {int d = 0;while (true) {if (vid[u] > vid[v]) std::swap(u, v);if (nxt[u] == nxt[v]) return d + vid[v] - vid[u];d += vid[v] - vid[nxt[v]] + 1;v = par[nxt[v]];}}int jump(int u, int v, int k) const {int d = dist(u, v);if (d < k) return -1;int l = lca(u, v);if (dist(u, l) >= k) return la(u, k);else return la(v, d - k);}int la(int v, int k) const {while (true) {int u = nxt[v];if (vid[v] - k >= vid[u]) return inv[vid[v] - k];k -= vid[v] - vid[u] + 1;v = par[u];}}int lca(int u, int v) const {while (true) {if (vid[u] > vid[v]) std::swap(u, v);if (nxt[u] == nxt[v]) return u;v = par[nxt[v]];}}template <class F>void for_each(int u, int v, const F &f) const {while (true) {if (vid[u] > vid[v]) std::swap(u, v);f(std::max(vid[nxt[v]], vid[u]), vid[v] + 1);if (nxt[u] != nxt[v]) v = par[nxt[v]];else break;}}template <class F>void for_each_edge(int u, int v, const F &f) const {while (true) {if (vid[u] > vid[v]) std::swap(u, v);if (nxt[u] != nxt[v]) {f(vid[nxt[v]], vid[v] + 1);v = par[nxt[v]];} else {if (u != v) f(vid[u] + 1, vid[v] + 1);break;}}}private:int _size;std::vector<int> vid, nxt, par, dep, inv;heavy_light_decomposition(int n) : _size(n), vid(n, -1), nxt(n), par(n, -1), dep(n), inv(n) {}};using Mint = modint107;struct S {Mint x, y;S operator+(const S &rhs) const {return S{x + rhs.x, y + rhs.y};}};struct F {Mint x;F operator+(const F &rhs) const {return F{x + rhs.x};}S operator+(const S &rhs) const {return S{rhs.x + x * rhs.y, rhs.y};}};int main(void) {int n;cin >> n;vector<int> s(n), c(n);cin >> s >> c;Graph<void> g(n);g.input_edges(n - 1);heavy_light_decomposition hld(g);vector<S> v(n);rep (i, n) v[hld.get(i)] = {s[i], c[i]};lazy_segment_tree<Add<S>, Add<F>> seg(v);int q;cin >> q;while (q--) {int t;cin >> t;if (t == 0) {int x, y, z;cin >> x >> y >> z;--x, --y;auto f = [&](int u, int v) {seg.apply(u, v, F{z});};hld.for_each(x, y, f);} else {int x, y;cin >> x >> y;--x, --y;Mint ans = 0;auto f = [&](int u, int v) {ans += seg.prod(u, v).x;};hld.for_each(x, y, f);co(ans);}}return 0;}