Tree#3710

Tree

1 second
122.486 megabytes
Medium

Given a rooted tree with integers assigned to its nodes, for each internal node, find the minimum absolute difference between numbers in any two distinct vertices within its subtree.

Input

The first line contains the number of vertices in the tree, (1 \le n \le 40000). The next lines each contain two integers: p_i and v_i (0 \le i < n). Here, p_i is the number of the parent of vertex . If p_i = -1, then this vertex is the root. The integer v_i is the value stored at vertex (0 \le v_i \le 10^6).

Output

Print a single number:

prb3710

Output the answer modulo . Here, the set refers to the indices of the internal vertices, and a_i is the answer for vertex . Use q = 127 and P = 1000000007.

Examples

Example 1

Loading
Loading