Tree Queries
Contest: Codeforces Round 601 (Div. 1)
Difficulty rating: 2700
Tags: data structures, probabilities, trees

Hanh is a famous biologist. He loves growing trees and doing experiments on his own garden.

One day, he got a tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. A tree with $$$n$$$ vertices is an undirected connected graph with $$$n-1$$$ edges. Initially, Hanh sets the value of every vertex to $$$0$$$.

Now, Hanh performs $$$q$$$ operations, each is either of the following types:

- Type $$$1$$$: Hanh selects a vertex $$$v$$$ and an integer $$$d$$$. Then he chooses some vertex $$$r$$$ uniformly at random, lists all vertices $$$u$$$ such that the path from $$$r$$$ to $$$u$$$ passes through $$$v$$$. Hanh then increases the value of all such vertices $$$u$$$ by $$$d$$$.
- Type $$$2$$$: Hanh selects a vertex $$$v$$$ and calculates the expected value of $$$v$$$.

Since Hanh is good at biology but not math, he needs your help on these operations.

Input format:
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n, q \leq 150\,000$$$) — the number of vertices on Hanh's tree and the number of operations he performs.

Each of the next $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$), denoting that there is an edge connecting two vertices $$$u$$$ and $$$v$$$. It is guaranteed that these $$$n - 1$$$ edges form a tree.

Each of the last $$$q$$$ lines describes an operation in either formats:

- $$$1$$$ $$$v$$$ $$$d$$$ ($$$1 \leq v \leq n, 0 \leq d \leq 10^7$$$), representing a first-type operation.
- $$$2$$$ $$$v$$$ ($$$1 \leq v \leq n$$$), representing a second-type operation.

It is guaranteed that there is at least one query of the second type.

Output format:
For each operation of the second type, write the expected value on a single line.

Let $$$M = 998244353$$$, it can be shown that the expected value can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.

Constraints:
Time limit: 5.0 s
Memory limit: 512.0 MB

Note:
The image below shows the tree in the example:

For the first query, where $$$v = 1$$$ and $$$d = 1$$$:

- If $$$r = 1$$$, the values of all vertices get increased.
- If $$$r = 2$$$, the values of vertices $$$1$$$ and $$$3$$$ get increased.
- If $$$r = 3$$$, the values of vertices $$$1$$$, $$$2$$$, $$$4$$$ and $$$5$$$ get increased.
- If $$$r = 4$$$, the values of vertices $$$1$$$ and $$$3$$$ get increased.
- If $$$r = 5$$$, the values of vertices $$$1$$$ and $$$3$$$ get increased.

Hence, the expected values of all vertices after this query are ($$$1, 0.4, 0.8, 0.4, 0.4$$$).

For the second query, where $$$v = 2$$$ and $$$d = 2$$$:

- If $$$r = 1$$$, the values of vertices $$$2$$$, $$$4$$$ and $$$5$$$ get increased.
- If $$$r = 2$$$, the values of all vertices get increased.
- If $$$r = 3$$$, the values of vertices $$$2$$$, $$$4$$$ and $$$5$$$ get increased.
- If $$$r = 4$$$, the values of vertices $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$ get increased.
- If $$$r = 5$$$, the values of vertices $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$ get increased.

Hence, the expected values of all vertices after this query are ($$$2.2, 2.4, 2, 2, 2$$$).

Example 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

Example output:
1
199648871
399297742
199648871
199648871
598946614
199648873
2
2
2

Editorial:
Disclaimer: Our other authors and testers have found better solutions; our best complexity is $$$O(n \times log^{2}(n))$$$. However, since this solution is the theoretically worst complexity that we intended to accept, I decided to write about it. Feel free to share your better solution in the comment section :)
Consider two distinct vertices $$$u$$$, $$$v$$$, the number of vertices $$$r$$$ such that the path from $$$r$$$ to $$$u$$$ passes through $$$v$$$ is $$$n - |Tree(v, u)|$$$, where $$$Tree(v, u)$$$ is the subtree we get by cutting the first edge on the path from $$$v$$$ to $$$u$$$, then keep the part with vertex $$$u$$$. If $$$u = v$$$ then this value will be $$$n$$$. By linearity of expectation, we can see that adding $$$d$$$ to $$$a[v]$$$ will add to the expected value of $$$a[u]$$$ an amount equal to $$$\frac{1}{n} \times d \times (n - |Tree(v, u)|)$$$. Note that this value is the same for all $$$u$$$ that lies on the same "subtree branch" of $$$v$$$. To update it efficiently, we can split the vertices into $$$2$$$ groups: those which has degree greater than $$$T$$$ (there are no more than $$$\frac{n}{T}$$$ of them), and those which does not. When we update a "light" vertex $$$v$$$, we iterate over the neighbors of $$$v$$$ and update the subtrees accordingly. When we get the value of a vertex $$$v$$$, we already have the sum of contributions from all "light" vertices to $$$v$$$, hence we can iterate over all "heavy" vertices and calculate the contribution from each of them to $$$v$$$.
If we use range-update point-query data structures such as Fenwick Trees then the complexity will be $$$O(Q \times log(n) \times (\frac{n}{T} + T)) = O(Q \times \sqrt{n} \times log(n))$$$ if we choose $$$T = O(\sqrt{n})$$$.