Dependent Events

아직 제출이 없습니다시간 제한60초메모리 제한1024 MB

문제

There are NN events, numbered 11 through NN. The probability of occurrence of each event depends upon the occurrence of exactly one other event called the parent event, except event 11, which is an independent event. In other words, for each event from 22 to NN33 values are given: P_iP\_i denoting the parent event of event iiA_iA\_i denoting the probability of occurrence of event ii if its parent event occurs, and B_iB\_i denoting the probability of occurrence of event ii if its parent event does not occur. For event 11, its probability of occurrence KK is given. There are QQ queries that we want to answer. Each query consists of 22 distinct events, u_ju\_j and v_jv\_j, and you need to find the probability that both events u_ju\_j and v_jv\_j have occurred.

입력

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

The first line of each test case contains two integers NN and QQ denoting the number of events and number of queries, respectively. NN lines follow. The ii-th line describes event ii. The first line contains a single integer KK denoting the probability of occurrence of event 11 multiplied by 10610^6. Each of the next N1N-1 lines consists of three integers P_iP\_iA_iA\_i and B_iB\_i denoting 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, respectively. Then, QQ lines follow, describing the queries. Each of these lines contains two distinct integers u_ju\_j and v_jv\_j. For each query, find the probability that both events u_ju\_j and v_jv\_j occurred.

출력

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 R_jR\_j is the sought probability computed for jj-th query modulo 109+710^9+7, which is defined precisely as follows. Represent the answer of jj-th query as an irreducible fraction pq\frac{p}{q}. The number R_jR\_j then must satisfy the modular equation R_j×qp(mod (109+7))R\_j × q ≡ p(\text{mod }(10^9+7)), and be between 00 and 109+610^9+6, inclusive. It can be shown that under the constraints of this problem such a number R_jR\_j always exists and is uniquely determined.

제한

  • 1T1001≤T≤100.
  • $1≤P_i
  • 1u_j,v_jN1≤u\_j,v\_j≤N and u_jv_ju\_j≠v\_j, for all jj.
  • 0A_i1060≤A\_i≤10^6, for each ii from 22 to NN.
  • 0B_i1060≤B\_i≤10^6, for each ii from 22 to NN.
  • 0K1060≤K≤10^6.