Under construction

Still being built. Things may change

Lucky Stars Management

Time limit2sMemory limit1024 MB

Summary
Given a company tree and expected fines E_i, decide whether the values are consistent and, if so, find Bill's minimum possible annual salary.
Level

Hard8 of 10

Topics
Tree, DFS, Dynamic programming, Math
Solved
No attempts yet

Problem

The company Lucky Stars has NN employees, numbered from 1 to NN in order of their position in the company. Bill, the President, has employee ID 1. Every employee except Bill has exactly one direct supervisor. The direct supervisor of employee ii is Pi<iP_i < i. The annual income AjA_j of each employee jj is an integer between 00 and 109+610^9+6.

One day Bill consulted a fortune-teller who is always right about the future. The fortune-teller said: "Someday, KK mistakes will be made, and Lucky Stars management will become difficult."

Each mistake is caused by exactly one employee. For i=1,2,…,Ni=1,2,\ldots,N, the probability that a mistake is caused by employee ii is 1/N1/N. Mistakes are independent, so one employee can cause several mistakes.

Bill decided to enforce personal responsibility in case the fortune-teller is right. When KK types of mistakes happen in one day, Bill wants to choose one employee to pay a fine. The employee is chosen by these rules.

  • When an employee makes a mistake, that employee and every superior above them (the direct supervisor, the supervisor's supervisor, and so on, up to Bill) are responsible.
  • Among the employees responsible for all KK types of mistakes, the employee with the largest number pays the fine.
  • If AjA_j is the annual salary of the employee who made the jj-th mistake for j=1,2,…,Kj=1,2,\ldots,K, the fine is A1×A2×…×AKA_1 \times A_2 \times \ldots \times A_K.

Bill calculated the expected fine EiE_i for employee ii, assuming the fortune-teller is right, and reported it modulo 109+710^9+7. For i=1,2,…,Ni=1,2,\ldots,N, write the expected fine as Yi/XiY_i/X_i, where XiX_i and YiY_i are coprime integers and XiX_i is not divisible by 109+710^9+7. The answer is YiY_i div XiX_i modulo 109+710^9+7.

Now you are given the company structure (PiP_i for each employee) and the expected fines EiE_i that Bill calculated. Decide whether all the given information could be correct. If so, find the minimum possible annual salary of Bill.

Input

The first line contains two integers NN (1≤N≤2⋅1051 \le N \le 2 \cdot 10^5) and KK (1≤K≤1091 \le K \le 10^9, KK is odd). The second line contains N−1N-1 integers P2,…,PNP_2, \ldots, P_N (1≤Pi≤i−11 \le P_i \le i-1), the direct supervisor of each employee ii. If N=1N=1, the second line is empty. The third line contains NN integers EiE_i (0≤Ei≤109+60 \le E_i \le 10^9+6), the calculated expected fines of the employees.

Output

If the calculated values are definitely wrong, print −1-1. Otherwise, print one integer: the minimum possible annual salary of Bill.

Examples2

  1. Example 1

    Input
    2 1
    1
    2 1
    
    Expected output
    4
    
  2. Example 2

    Input
    1 1
    
    2
    
    Expected output
    2