This page is still under construction.

Parts of this page are still being built. What you see may change.

Dependent Events

Time limit60sMemory limit1024 MB

Summary
A rooted tree of events gives each node a conditional probability, and queries ask for the joint probability that two given nodes both occur, modulo 1e9+7.
Level

Medium7 of 10

Topics
Tree, Probability, Math, DFS
Solved
No attempts yet

Problem

There are NN events, numbered 11 through NN. The probability that each event occurs depends on whether exactly one other event, called its parent event, occurs. Event 11 is the exception: it is an independent event. That is, for each event ii from 22 to NN, three values are given: PiP_i, the parent event of event ii; AiA_i, the probability that event ii occurs if its parent event occurs; and BiB_i, the probability that event ii occurs if its parent event does not occur. For event 11, its probability of occurrence KK is given. There are QQ queries to answer. Each query consists of two distinct events uju_j and vjv_j, and you need to find the probability that both events uju_j and vjv_j have occurred.

Input

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains two integers NN and QQ, the number of events and the number of queries. NN lines follow. The ii-th line describes event ii. The first line contains a single integer KK, the probability of occurrence of event 11 multiplied by 10610^6. Each of the next N−1N-1 lines consists of three integers PiP_i, AiA_i, and BiB_i: the parent event of event ii, the probability of occurrence of event ii if its parent event occurs multiplied by 10610^6, and the probability of occurrence of event ii if its parent event does not occur multiplied by 10610^6. Then QQ lines follow, describing the queries. Each of these lines contains two distinct integers uju_j and vjv_j. For each query, find the probability that both events uju_j and vjv_j occurred.

Output

For each test case, output one line containing Case #x: R1 R2 R3 … RQ, where xx is the test case number (starting from 1) and RjR_j is the sought probability computed for the jj-th query modulo 109+710^9+7, defined precisely as follows. Represent the answer of the jj-th query as an irreducible fraction pq\frac{p}{q}. The number RjR_j then must satisfy the modular equation Rj×q≡p(mod109+7)R_j × q ≡ p \pmod{10^9+7} and be between 00 and 109+610^9+6, inclusive. Under the constraints of this problem such a number RjR_j always exists and is uniquely determined.

Constraints

  • 1≤T≤1001 ≤ T ≤ 100.
  • 1≤Pi1 ≤ P_i.
  • 1≤uj,vj≤N1 ≤ u_j, v_j ≤ N and uj≠vju_j ≠ v_j, for all jj.
  • 0≤Ai≤1060 ≤ A_i ≤ 10^6, for each ii from 22 to NN.
  • 0≤Bi≤1060 ≤ B_i ≤ 10^6, for each ii from 22 to NN.
  • 0≤K≤1060 ≤ K ≤ 10^6.

Examples1

  1. Example 1

    Input
    2
    5 2
    200000
    1 400000 300000
    2 500000 200000
    1 800000 100000
    4 200000 400000
    1 5
    3 5
    4 2
    300000
    1 100000 100000
    2 300000 400000
    3 500000 600000
    1 2
    2 4
    
    Expected output
    Case #1: 136000001 556640004
    Case #2: 710000005 849000006