Expected Distance
Time limit4sMemory limit1024 MB
Each vertex attaches to an earlier vertex with probability proportional to a_j. For each query (u, v), output the expected distance between them modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Tree, Probability, Math
- Solved
- No attempts yet
Problem
Build a tree with vertices as follows.
- is a tree that consists only of vertex 1.
- For each with , is the tree made by adding vertex to with an edge to one of the vertices of . The probability that vertex connects to vertex is . If vertex connects to vertex , the length of that edge is .
You are given queries. For each query with and , find the expected distance between vertex and vertex in .
Input
The first line contains and .
The second line contains integers , separated by spaces.
The third line contains integers , separated by spaces.
Each of the next lines contains and , separated by a space.
Output
Let the answer to the -th query be , where and are coprime non-negative integers. On the -th line, print the integer with that satisfies . It can be proven that such is unique.