Tree
Given a tree with integers assigned to its nodes, your task is to compute the minimum absolute difference between numbers in any two distinct vertices within the subtree of each internal node.
Input
The first line contains the number of vertices in the tree, n (1 ≤ n ≤ 40000). The next n lines each contain a pair of integers p[i]
and v[i]
(0 ≤ i < n). Here, p[i]
represents the parent of the vertex numbered i. If p[i]
= -1, then this vertex is the root of the tree. The integer v[i]
is the value stored at vertex i (0 ≤ v[i]
≤ 10^6).
Output
Print a single number, which is the result of the computation described below.
The output should be computed modulo P. Here, i refers to the set of indices of internal vertices, and a[i]
are the computed answers for these vertices. Use q = 127 and P = 1000000007 for the calculations.