logo AlgoBeat OnlineJudge
登录 注册

#216183. [ICPC 2019 NAIPC] Heaps of Fun

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Consider a rooted tree with nodes, numbered . Each node will have a fixed integer , and for each, a uniform random real number is chosen in the interval .

What is the probability that the random numbers chosen cause the tree to form a Heap (i.e., the random value in each node is less than the random values in its children)?

This probability can always be expressed as a rational number , with . You are to output the probability as , where is an integer, which is the multiplicative inverse of modulo (). (Note: does not depend on whether and are relatively prime, only on their ratio .)

输入格式

Each test case will begin with a line with a single integer (), which is the number of nodes in the tree.

Each of the next lines will contain a pair of space-separated integers () and () describing a node of the tree, where is the fixed integer value in the node and is the node number of its parent. The nodes are listed in order; node 1 is first, then node 2, and so on. A single node will have a parent . This is the root of the tree.

输出格式

Output a single integer, which is the probability expressed as .

样例

样例输入 1

2
1000000000 0
1000000000 1

样例输出 1

500000004

样例输入 2

5
2 3
2 3
1 0
2 3
2 3

样例输出 2

87500001