#line 2 "/root/AtCoder/Halc-Library/DataStructure/SegmentTree.hpp" #include #include #include #include template struct SegmentTree { using T = typename M::T; int32_t siz; std::vector tree; SegmentTree(int32_t sz) { siz = sz; tree = std::vector(siz << 1, M::e); } SegmentTree(std::vector def) { siz = def.size(); tree = std::vector(siz << 1, M::e); for (int32_t i = 0; i < siz; i++) { tree[i + siz] = def[i]; } for (int32_t i = siz - 1; i > 0; i--) { tree[i] = M::op(tree[i << 1], tree[(i << 1) + 1]); } } void set(int32_t p, T v) { p += siz; tree[p] = v; p >>= 1; while (p > 0) { tree[p] = M::op(tree[p << 1], tree[(p << 1) + 1]); p >>= 1; } } T get(int32_t p) { return tree[p + siz]; } T prod(int32_t lf, int32_t ri) { lf += siz; ri += siz; T rel = M::e; T rer = M::e; while (lf < ri) { if (lf & 1) { rel = M::op(rel, tree[lf]); lf++; } if (ri & 1) { ri--; rer = M::op(tree[ri], rer); } lf >>= 1; ri >>= 1; } return M::op(rel, rer); } template int32_t max_right(int lf) { return max_right(lf, [](T x) { return f(x); }); } template int32_t max_right(int32_t lf, F f) { lf += siz; int32_t ri = siz << 1; std::queue lfp; std::stack rip; while (lf < ri) { if (lf & 1) { lfp.push(lf); lf++; } if (ri & 1) { ri--; rip.push(ri); } lf >>= 1; ri >>= 1; } T val = M::e; while (!lfp.empty()) { int32_t i = lfp.front(); lfp.pop(); if (!f(M::op(val, tree[i]))) { while (i < siz) { i <<= 1; if (f(M::op(val, tree[i]))) { val = M::op(val, tree[i]); i++; } } return i - siz; } val = M::op(val, tree[i]); } while (!rip.empty()) { int32_t i = rip.top(); rip.pop(); if (!f(M::op(val, tree[i]))) { while (i < siz) { i <<= 1; if (f(M::op(val, tree[i]))) { val = M::op(val, tree[i]); i++; } } return i - siz; } val = M::op(val, tree[i]); } return siz; } template int32_t min_left(int ri) { return min_left(ri, [](T x) { return f(x); }); } template int32_t min_left(int32_t ri, F f) { ri += siz; int32_t lf = siz; std::queue rip; std::stack lfp; while (lf < ri) { if (lf & 1) { lfp.push(lf); lf++; } if (ri & 1) { ri--; rip.push(ri); } lf >>= 1; ri >>= 1; } T val = M::e; while (!rip.empty()) { int32_t i = rip.front(); rip.pop(); if (!f(M::op(val, tree[i]))) { while (i < siz) { i <<= 1; i++; if (f(M::op(tree[i], val))) { val = M::op(tree[i], val); i--; } } return i - siz + 1; } val = M::op(tree[i], val); } while (!lfp.empty()) { int32_t i = lfp.top(); lfp.pop(); if (!f(M::op(val, tree[i]))) { while (i < siz) { i <<= 1; i++; if (f(M::op(tree[i], val))) { val = M::op(tree[i], val); i--; } } return i - siz + 1; } val = M::op(tree[i], val); } return 0; } int32_t size() { return siz; } }; #line 4 "/root/AtCoder/Halc-Library/Graph/Graph.hpp" template struct Edge { int32_t from, to; T cost; int32_t idx; Edge() = default; Edge(int32_t from, int32_t to, T cost = 1, int32_t idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int32_t() { return to; } void reverse() { std::swap(from, to); } }; template struct Graph { std::vector>> gr; int32_t eds = 0; Graph() = default; Graph(int32_t n) { gr.resize(n); } void add_edge(int32_t from, int32_t to, T cost = 1, bool directed = false) { gr[from].emplace_back(from, to, cost, eds); if (!directed) { gr[to].emplace_back(to, from, cost, eds); } eds++; } void add_directed_edge(int32_t from, int32_t to, T cost = 1) { gr[from].emplace_back(from, to, cost, eds); eds++; } inline std::vector> &operator[](const int32_t &p) { return gr[p]; } int32_t size() { return gr.size(); } }; template Graph reverse_edges(Graph &gr) { Graph ret(gr.size()); for (int32_t i = 0; i < gr.size(); i++) { for (Edge j : gr[i]) { ret[j].emplace_back(j); ret[j].back().reverse(); } } return ret; } #line 3 "/root/AtCoder/Halc-Library/Modint/Modint.hpp" #include template struct Modint { uint64_t x; constexpr Modint() noexcept { x = 0; } constexpr Modint(int64_t val) noexcept { x = (val < 0 ? val % (int64_t)(Mod) + Mod : val % Mod); } inline uint64_t _get_mod(uint64_t val) noexcept { const static uint64_t m_inv = (-1ULL) / Mod + 1; uint64_t ret = ((unsigned __int128)(val)*m_inv) >> 64; uint64_t pro = ret * Mod; return (val - pro + (val < pro ? Mod : 0)); } friend std::ostream &operator<<(std::ostream &os, Modint &b) { return os << b.x; } friend std::istream &operator>>(std::istream &is, Modint &b) { return is >> b.x; } constexpr uint64_t val() noexcept { return x; } constexpr Modint operator+() noexcept { return (*this); } constexpr Modint operator-() noexcept { return Modint() - (*this); } constexpr Modint operator+(const Modint rhs) noexcept { return Modint(*this) += rhs; } constexpr Modint operator-(const Modint rhs) noexcept { return Modint(*this) -= rhs; } constexpr Modint operator*(const Modint rhs) noexcept { return Modint(*this) *= rhs; } constexpr Modint operator/(const Modint rhs) noexcept { return Modint(*this) /= rhs; } constexpr Modint &operator+=(const Modint rhs) noexcept { x += rhs.x; if (x >= Mod) x -= Mod; return *this; } constexpr Modint &operator-=(const Modint rhs) noexcept { if (x < rhs.x) x += Mod; x -= rhs.x; return *this; } constexpr Modint &operator*=(const Modint rhs) noexcept { x = _get_mod(x * rhs.x); return *this; } constexpr bool operator==(Modint rhs) noexcept { return x == rhs.x; } constexpr bool operator!=(Modint rhs) noexcept { return x != rhs.x; } constexpr Modint &operator/=(Modint rhs) noexcept { return (*this) *= rhs.inv(); } constexpr Modint inv() noexcept { return (*this).pow(Mod - 2); } constexpr Modint pow(uint64_t x) noexcept { Modint ret = 1; Modint bin = (*this); while (x) { if (x & 1) ret *= bin; bin *= bin; x >>= 1; } return ret; } static uint64_t get_mod() noexcept { return Mod; } }; template struct ArbitraryModint { uint64_t x; static uint64_t &mod() noexcept { static uint64_t Mod = 0; return Mod; } constexpr ArbitraryModint() noexcept { x = 0; } constexpr ArbitraryModint(int64_t val) noexcept { x = (val < 0 ? val % (int64_t)(get_mod()) + get_mod() : val % get_mod()); } inline uint64_t _get_mod(uint64_t val) noexcept { const static uint64_t m_inv = (-1ULL) / get_mod() + 1; uint64_t ret = ((unsigned __int128)(val)*m_inv) >> 64; uint64_t pro = ret * get_mod(); return (val - pro + (val < pro ? get_mod() : 0)); } friend std::ostream &operator<<(std::ostream &os, ArbitraryModint &b) { return os << b.x; } friend std::istream &operator>>(std::istream &is, ArbitraryModint &b) { return is >> b.x; } constexpr uint64_t val() noexcept { return x; } constexpr ArbitraryModint operator+() noexcept { return (*this); } constexpr ArbitraryModint operator-() noexcept { return ArbitraryModint() - (*this); } constexpr ArbitraryModint operator+(const ArbitraryModint rhs) noexcept { return ArbitraryModint(*this) += rhs; } constexpr ArbitraryModint operator-(const ArbitraryModint rhs) noexcept { return ArbitraryModint(*this) -= rhs; } constexpr ArbitraryModint operator*(const ArbitraryModint rhs) noexcept { return ArbitraryModint(*this) *= rhs; } constexpr ArbitraryModint operator/(const ArbitraryModint rhs) noexcept { return ArbitraryModint(*this) /= rhs; } constexpr ArbitraryModint &operator+=(const ArbitraryModint rhs) noexcept { x += rhs.x; if (x >= get_mod()) x -= get_mod(); return *this; } constexpr ArbitraryModint &operator-=(const ArbitraryModint rhs) noexcept { if (x < rhs.x) x += get_mod(); x -= rhs.x; return *this; } constexpr ArbitraryModint &operator*=(const ArbitraryModint rhs) noexcept { x = _get_mod(x * rhs.x); return *this; } constexpr ArbitraryModint &operator/=(ArbitraryModint rhs) noexcept { return (*this) *= rhs.inv(); } constexpr bool operator==(ArbitraryModint rhs) noexcept { return x == rhs.x; } constexpr bool operator!=(ArbitraryModint rhs) noexcept { return x != rhs.x; } constexpr ArbitraryModint inv() noexcept { return (*this).pow(get_mod() - 2); } constexpr ArbitraryModint pow(uint64_t x) noexcept { ArbitraryModint ret = 1; ArbitraryModint bin = (*this); while (x) { if (x & 1) ret *= bin; bin *= bin; x >>= 1; } return ret; } static void set_mod(const uint64_t x) noexcept { mod() = x; } static uint64_t get_mod() noexcept { return mod(); } }; template inline void scan(Modint &a) { std::cin >> a.x; } template inline void scan(ArbitraryModint &a) { std::cin >> a.x; } template inline void print(Modint a) { std::cout << a.x; } template inline void print(ArbitraryModint a) { std::cout << a.x; } #line 5 "/root/AtCoder/Halc-Library/Tree/HLDecomposition.hpp" #line 7 "/root/AtCoder/Halc-Library/Tree/HLDecomposition.hpp" struct HLDecomposition { struct Segment { int32_t lf, ri; bool rev; }; int32_t sz; std::vector tree_sz; std::vector depth; std::vector order; std::vector path_roots; std::vector parent; std::vector out; template void _build(int32_t pos, Graph &tree) { order[pos] = sz; sz++; int32_t mx = -1, mp = -1; for (int32_t i : tree[pos]) { if (i == parent[pos]) continue; if (mx < tree_sz[i]) { mx = tree_sz[i]; mp = i; } } if (mx == -1) { out[pos] = sz; return; } path_roots[mp] = path_roots[pos]; _build(mp, tree); for (int32_t i : tree[pos]) { if (i == parent[pos]) continue; if (i == mp) continue; path_roots[i] = i; _build(i, tree); } out[pos] = sz; } template int32_t _calc_sz(int32_t pos, Graph &tree) { if (tree_sz[pos] != -1) return tree_sz[pos]; tree_sz[pos] = 1; for (int32_t i : tree[pos]) { if (parent[pos] != i) { parent[i] = pos; depth[i] = depth[pos] + 1; tree_sz[pos] += _calc_sz(i, tree); } } return tree_sz[pos]; } template HLDecomposition(Graph &tree, int32_t root = 0) { sz = tree.size(); tree_sz.resize(sz, -1); depth.resize(sz, -1); parent.resize(sz, -1); depth[root] = 0; _calc_sz(root, tree); order.resize(sz, -1); out.resize(sz, -1); path_roots.resize(sz, -1); sz = 0; path_roots[root] = root; _build(root, tree); } int32_t operator[](int32_t p) { return order[p]; } Segment subtree(int32_t pos) { return {order[pos], out[pos], false}; } std::vector path(int32_t s, int32_t t) { std::vector ret; std::stack right; while (path_roots[s] != path_roots[t]) { if (depth[path_roots[s]] > depth[path_roots[t]]) { ret.emplace_back( Segment{order[path_roots[s]], order[s] + 1, true}); s = parent[path_roots[s]]; } else { right.push({order[path_roots[t]], order[t] + 1, false}); t = parent[path_roots[t]]; } } if (depth[s] < depth[t]) { ret.emplace_back(Segment{order[s], order[t] + 1, false}); } else { ret.emplace_back(Segment{order[t], order[s] + 1, true}); } while (!right.empty()) { ret.push_back(right.top()); right.pop(); } return ret; } int32_t lca(int32_t s, int32_t t) { while (path_roots[s] != path_roots[t]) { if (depth[path_roots[s]] > depth[path_roots[t]]) { s = parent[path_roots[s]]; } else { t = parent[path_roots[t]]; } } if (depth[s] < depth[t]) return s; return t; } }; #line 2 "/root/AtCoder/Halc-Library/Template/Template.hpp" #include using namespace std; #line 8 "/root/AtCoder/Halc-Library/Template/InOut.hpp" inline void scan() {} inline void scan(int &a) { std::cin >> a; } inline void scan(unsigned &a) { std::cin >> a; } inline void scan(long &a) { std::cin >> a; } inline void scan(long long &a) { std::cin >> a; } inline void scan(unsigned long long &a) { std::cin >> a; } inline void scan(char &a) { std::cin >> a; } inline void scan(float &a) { std::cin >> a; } inline void scan(double &a) { std::cin >> a; } inline void scan(long double &a) { std::cin >> a; } inline void scan(std::vector &vec) { for (int32_t i = 0; i < vec.size(); i++) { int a; scan(a); vec[i] = a; } } inline void scan(std::string &a) { std::cin >> a; } template inline void scan(std::vector &vec); template inline void scan(std::array &vec); template inline void scan(std::pair &p); template inline void scan(T (&vec)[size]); template inline void scan(std::vector &vec) { for (auto &i : vec) scan(i); } template inline void scan(std::deque &vec) { for (auto &i : vec) scan(i); } template inline void scan(std::array &vec) { for (auto &i : vec) scan(i); } template inline void scan(std::pair &p) { scan(p.first); scan(p.second); } template inline void scan(T (&vec)[size]) { for (auto &i : vec) scan(i); } template inline void scan(T &a) { std::cin >> a; } inline void in() {} template inline void in(Head &head, Tail &...tail) { scan(head); in(tail...); } inline void print() { std::cout << ' '; } inline void print(const bool &a) { std::cout << a; } inline void print(const int &a) { std::cout << a; } inline void print(const unsigned &a) { std::cout << a; } inline void print(const long &a) { std::cout << a; } inline void print(const long long &a) { std::cout << a; } inline void print(const unsigned long long &a) { std::cout << a; } inline void print(const char &a) { std::cout << a; } inline void print(const char a[]) { std::cout << a; } inline void print(const float &a) { std::cout << a; } inline void print(const double &a) { std::cout << a; } inline void print(const long double &a) { std::cout << a; } inline void print(const std::string &a) { for (auto &&i : a) print(i); } template inline void print(const std::vector &vec); template inline void print(const std::array &vec); template inline void print(const std::pair &p); template inline void print(const T (&vec)[size]); template inline void print(const std::vector &vec) { if (vec.empty()) return; print(vec[0]); for (auto i = vec.begin(); ++i != vec.end();) { std::cout << ' '; print(*i); } } template inline void print(const std::deque &vec) { if (vec.empty()) return; print(vec[0]); for (auto i = vec.begin(); ++i != vec.end();) { std::cout << ' '; print(*i); } } template inline void print(const std::array &vec) { print(vec[0]); for (auto i = vec.begin(); ++i != vec.end();) { std::cout << ' '; print(*i); } } template inline void print(const std::pair &p) { print(p.first); std::cout << ' '; print(p.second); } template inline void print(const T (&vec)[size]) { print(vec[0]); for (auto i = vec; ++i != end(vec);) { std::cout << ' '; print(*i); } } template inline void print(const T &a) { std::cout << a; } inline void out() { std::cout << '\n'; } template inline void out(const T &t) { print(t); std::cout << '\n'; } template inline void out(const Head &head, const Tail &...tail) { print(head); std::cout << ' '; out(tail...); } inline void Yes(bool i = true) { out(i ? "Yes" : "No"); } inline void No(bool i = true) { out(i ? "No" : "Yes"); } struct IOsetup { IOsetup() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::setprecision(10); } } iosetup; #line 8 "/root/AtCoder/Halc-Library/Template/Util.hpp" using ll = long long; using ld = long double; using ull = unsigned long long; using uint = unsigned int; using pll = std::pair; using pii = std::pair; using vl = std::vector; using vvl = std::vector>; using pdd = std::pair; using tuplis = std::array; template using pq = std::priority_queue, std::greater>; constexpr ll LINF = (1LL << 62) - (1LL << 31); constexpr int32_t INF = INT_MAX >> 1; constexpr ll MINF = 1LL << 40; constexpr ld DINF = std::numeric_limits::infinity(); constexpr int32_t MODD = 1000000007; constexpr int32_t MOD = 998244353; constexpr ld EPS = 1e-9; constexpr ld PI = 3.1415926535897932; const ll four[] = {0, 1, 0, -1, 0}; const ll eight[] = {0, 1, 1, 0, -1, -1, 1, -1, 0}; template bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } else return false; } template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } else return false; } template ll sum(const T &a) { return accumulate(std::begin(a), std::end(a), 0LL); } template ld dsum(const T &a) { return accumulate(std::begin(a), std::end(a), 0.0L); } template auto min(const T &a) { return *min_element(std::begin(a), std::end(a)); } template auto max(const T &a) { return *max_element(std::begin(a), std::end(a)); } #line 1 "/root/AtCoder/Halc-Library/Template/Macro.hpp" #define _overload3(_1, _2, _3, name, ...) name #define _overload4(_1, _2, _3, _4, name, ...) name #define _rep1(i, n) for (ll i = 0; i < (n); i++) #define _rep2(i, a, b) for (ll i = (a); i < (b); i++) #define _rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c)) #define rep(...) _overload4(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__) #define _rrep1(i, n) for (ll i = (n); i-- > 0;) #define _rrep2(i, a, b) for (ll i = (b); i-- > (a);) #define rrep(...) _overload3(__VA_ARGS__, _rrep2, _rrep1)(__VA_ARGS__) #define each(i, ...) for (auto&& i : __VA_ARGS__) #define all(i) std::begin(i), std::end(i) #define rall(i) std::rbegin(i), std::rend(i) #define len(x) ((int)(x).size()) #define fi first #define se second #define uniq(x) x.erase(unique(all(x)), std::end(x)) #define vec(type, name, ...) vector name(__VA_ARGS__); #define vv(type, name, h, ...) std::vector> name(h, std::vector(__VA_ARGS__)); #define INT(...) int __VA_ARGS__; in(__VA_ARGS__) #define LL(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ULL(...) unsigned long long __VA_ARGS__; in(__VA_ARGS__) #define STR(...) std::string __VA_ARGS__; in(__VA_ARGS__) #define CHR(...) char __VA_ARGS__; in(__VA_ARGS__) #define LD(...) long double __VA_ARGS__; in(__VA_ARGS__) #define VEC(type, name, size) std::vector name(size); in(name) #define VV(type, name, h, w) std::vector> name(h, std::vector(w)); in(name) #line 6 "main.cpp" using mint = Modint; struct mat { using T = array; static T op(T x, T y) { return { x[0] * y[0] + x[1] * y[2], x[0] * y[1] + x[1] * y[3], x[2] * y[0] + x[3] * y[2], x[2] * y[1] + x[3] * y[3] }; } static inline T e = {1, 0, 0, 1}; }; void solve() { LL(n); Graph gr(n); rep(i, n - 1) { LL(u, v); gr.add_edge(u, v); } HLDecomposition hld(gr); SegmentTree seg(n-1); vec(ll,change,n); rep(i,n){ each(j,gr[i]){ if(hld.depth[i]