Under construction

Still being built. Things may change

Expected Distance

Time limit4sMemory limit1024 MB

Summary
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 TNT_{N} with NN vertices as follows.

  • T1T_{1} is a tree that consists only of vertex 1.
  • For each ii with 2≤i2 \le i, TiT_{i} is the tree made by adding vertex ii to Ti−1T_{i-1} with an edge to one of the vertices of Ti−1T_{i-1}. The probability that vertex ii connects to vertex jj is aja1+⋯+ai−1\frac{a_j}{a_1+ \cdots + a_{i-1}}. If vertex ii connects to vertex jj, the length of that edge is ci+cjc_i + c_j.

You are given QQ queries. For each query with uu and vv, find the expected distance between vertex uu and vertex vv in TNT_{N}.

Input

The first line contains NN and QQ. (2≤N,Q≤300 000)(2 \le N, Q \le 300\,000)

The second line contains N−1N-1 integers a1,a2,⋯ ,aN−1a_1, a_2, \cdots, a_{N-1}, separated by spaces. (1≤ai≤2 000)(1 \le a_i \le 2\,000)

The third line contains NN integers c1,c2,⋯ ,cNc_1, c_2, \cdots, c_N, separated by spaces. (1≤ci≤2 000)(1 \le c_i \le 2\,000)

Each of the next QQ lines contains uu and vv, separated by a space. (1≤u,v≤N)(1 \le u, v \le N)

Output

Let the answer to the ii-th query be ansi=piqians_i=\frac{p_i}{q_i}, where pip_i and qiq_i are coprime non-negative integers. On the ii-th line, print the integer xix_i with 0≤xi<109+70 \le x_i < 10^9+7 that satisfies pi≡qixi(mod109+7)p_i \equiv q_i x_i \pmod{10^9+7}. It can be proven that such xix_i is unique.

Examples2

  1. Example 1

    Input
    5 7
    1 1 1 1
    1 2 4 8 16
    1 3
    2 5
    4 3
    1 5
    3 3
    4 5
    1 2
    
    Expected output
    7
    666666697
    15
    666666697
    0
    333333366
    3
    
  2. Example 2

    Input
    5 4
    17 19 23 29
    2 3 5 7 11
    1 2
    3 4
    5 2
    3 5
    
    Expected output
    5
    927495315
    106531441
    450222593