Power Tree
Contest: Codeforces Round 336 (Div. 1)
Difficulty rating: 2600
Tags: data structures, trees

Genos and Saitama went shopping for Christmas trees. However, a different type of tree caught their attention, the exalted Power Tree.

A Power Tree starts out as a single root vertex indexed 1. A Power Tree grows through a magical phenomenon known as an update. In an update, a single vertex is added to the tree as a child of some other vertex.

Every vertex in the tree (the root and all the added vertices) has some value vi associated with it. The power of a vertex is defined as the strength of the multiset composed of the value associated with this vertex (vi) and the powers of its direct children. The strength of a multiset is defined as the sum of all elements in the multiset multiplied by the number of elements in it. Or in other words for some multiset S:

$$Strength(S) = |S| \cdot \sum_{d \in S} d$$

Saitama knows the updates that will be performed on the tree, so he decided to test Genos by asking him queries about the tree during its growth cycle.

An update is of the form 1 p v, and adds a new vertex with value v as a child of vertex p.

A query is of the form 2 u, and asks for the power of vertex u.

Please help Genos respond to these queries modulo 109 + 7.

Input format:
The first line of the input contains two space separated integers v1 and q (1 ≤ v1 < 109, 1 ≤ q ≤ 200 000) — the value of vertex 1 and the total number of updates and queries respectively.

The next q lines contain the updates and queries. Each of them has one of the following forms:

- 1 pi vi, if these line describes an update. The index of the added vertex is equal to the smallest positive integer not yet used as an index in the tree. It is guaranteed that pi is some already existing vertex and 1 ≤ vi < 109.
- 2 ui, if these line describes a query. It is guaranteed ui will exist in the tree.

It is guaranteed that the input will contain at least one query.

Output format:
For each query, print out the power of the given vertex modulo 109 + 7.

Constraints:
Time limit: 3.5 s
Memory limit: 256.0 MB

Note:
For the first sample case, after all the updates the graph will have vertices labelled in the following manner: 1 — 2 — 3 — 4 — 5

These vertices will have corresponding values: 2 — 3 — 5 — 7 — 11

And corresponding powers: 344 — 170 — 82 — 36 — 11

Example input:
2 5
1 1 3
1 2 5
1 3 7
1 4 11
2 1

Example output:
344

Example input:
5 5
1 1 4
1 2 3
2 2
1 2 7
2 1

Example output:
14
94

Editorial:
Let's solve a restricted version of the problem where all queries are about the root. First however, let us define some notation. In this editorial, we will use d(x) to denote the number of children of vertex x. If there is an update involved, d(x) refers to the value prior to the update.
To deal these queries, notice that each vertex within the tree has some contribution ci to the root power. This contribution is an integer multiple mi of each vertex's value vi, such that ci = mi·vi If we sum the contributions of every vertex, we get the power of the root.
To deal with updates, notice that adding a vertex u to a leaf p scales the multiplier of every vertex in p's subtree by a factor of $$\frac{d(p)+2}{d(p)+1}$$. As for the contribution of u, notice that mu = mp.
Now, in order to handle both queries and updates efficiently, we need a fast way to sum all contributions, a way to scale contributions in a subtree, and a way to add new vertices. This sounds like a job for ... a segment tree!
We all know segment trees hate insertions, so instead of inserting new vertices, we pre-build the tree with initial values 0, updating values instead of inserting new vertices. In order to efficiently support subtree modification, we construct a segment tree on the preorder walk of the tree, so that every subtree corresponds to a contiguous segment within the segment tree. This segment tree will store the contributions of each vertex and needs to support range-sum-query, range-multiply-update, and point-update (updating a single element). The details of implementing such a segment tree and are left as an exercise to the reader.
Armed with this segment tree, queries become a single range-sum. Scaling the contribution in a subtree becomes a range-multiply (we don't need to worry about multiplying un-added vertices because they are set to 0). And adding a new vertex becomes a range-sum-query to retrieve the contribution of the parent, and then a point-set to set the contribution of the added vertex.
Finally, to solve the full version of the problem, notice that the power of a non-root vertex w is a scaled down range sum in the segment tree. The value of the scale is $$\frac{d(w)+1}{m_w}$$, the proof of which is left as an exercise to the reader.
Time Complexity - $${ \mathcal { O } } ( q \cdot \log q )$$, Space Complexity - O(q)