PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000104. Truy vấn kỳ vọng trên cây (Tree Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn kỳ vọng trên cây (Tree Queries)

Tree Queries

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected connected tree with nn vertices numbered 11 through nn and exactly n−1n-1 edges. All vertex values start at 00. Process qq operations in order:

  • 1 v d: choose a vertex rr uniformly at random from all nn vertices. For every vertex uu such that the simple path from rr to uu passes through vv (including either endpoint), increase the value at uu by dd. A fresh random root is chosen for each update; the goal is to calculate expectations, not simulate random samples.
  • 2 v: compute the expected value currently stored at vertex vv after all preceding updates.

Output results modulo M=998244353M=998244353. An expected value written as a fraction p/qp/q with q≢0(modM)q\not\equiv0\pmod M is represented by p⋅q−1 mod Mp\cdot q^{-1}\bmod M, where q−1q^{-1} is the multiplicative inverse of qq modulo MM.

Input

  • The first line contains n,qn,q.
  • The next n−1n-1 lines describe undirected edges u v, forming a tree.
  • The last qq lines have the format 1 v d or 2 v. At least one type-2 operation is guaranteed.

Output

For each type-2 query, output one integer xx with 0≤x<M0\le x<M, representing the expected value modulo MM.

Subtasks

  • Subtask 1 (10 points): 1≤n≤301\le n\le30, 1≤q≤801\le q\le80; n−1n-1 undirected edges form a tree; 1≤v≤n1\le v\le n, 0≤d≤1070\le d\le10^7; at least one type-2 query; M=998244353M=998244353.
  • Subtask 2 (20 points): 1≤n,q≤1051\le n,q\le10^5; n−1n-1 undirected edges form a tree; every type-1 operation updates v=1v=1; 0≤d≤1070\le d\le10^7; at least one type-2 query; M=998244353M=998244353.
  • Subtask 3 (30 points): 1≤n,q≤150001\le n,q\le15000; n−1n-1 undirected edges form a tree; 1≤v≤n1\le v\le n, 0≤d≤1070\le d\le10^7; at least one type-2 query; M=998244353M=998244353.
  • Subtask 4 (40 points): 1≤n,q≤1500001\le n,q\le150000; n−1n-1 undirected edges form a tree; 1≤v≤n1\le v\le n, 0≤d≤1070\le d\le10^7; at least one type-2 query; M=998244353M=998244353.

Examples

Input

5 12
1 2
1 3
2 4
2 5
1 1 1
2 1
2 2
2 3
2 4
2 5
1 2 2
2 1
2 2
2 3
2 4
2 5

Output

1
199648871
399297742
199648871
199648871
598946614
199648873
2
2
2

Explanation

After 1 1 1, averaging over five possible random roots yields expected values (1,2/5,4/5,2/5,2/5)(1,2/5,4/5,2/5,2/5). In the given modulus, 2/5=1996488712/5=199648871 and 4/5=3992977424/5=399297742. After 1 2 2, the new expected values are (11/5,12/5,2,2,2)(11/5,12/5,2,2,2); the final five queries output the modular representations of these values.