Reasonable Workplace Relationship
Time limit2sMemory limit256 MB
For each query node, count over its subtree the expected number of leaders who are happy, where a node is happy when its subtree fraction of members exceeding a_i + w_i is zero, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- DFS, Prefix sum, Sorting, Combinatorics
- Solved
- No attempts yet
Problem
Subordinates must obey the orders of their superiors unconditionally. If they have any objection, they can raise it later. "If you throw the pot up and down at your subordinates, they can only accept it with patience and full of grievances"... Do these words often seen in workplace novels really reflect the current situation? Let us turn to a more specific simplified model.
Now, we have a workplace relationship model, which is a superior-subordinate relationship. Let us assume there is only one big boss. Since they are called the big boss, they naturally have no superior. The big boss has some subordinates who are directly managed by them, and these subordinates have their own subordinates... Repeat this several times, and we can clearly see that this model is a rooted tree. There are people in the model, numbered from to . The superior of person is the parent of node , and the big boss is the root of the tree. We give each person an integer to measure their ability.
A group consists of a person, called the leader of the group, along with all their direct and indirect subordinates. Clearly, a group corresponds to some subtree, and the group leader is the root of that subtree. We know that the management of subordinates by leaders should not be based only on the ability of leaders and subordinates, which is too narrow and not conducive to management. Because of their special status as leaders, leaders obviously still have the so-called prestige to help them manage. So we give each person an integer to measure their prestige.
With prestige and certain ability, leadership can convince the public. Unfortunately, there are always some subordinates whose ability value () is greater than the sum of the leader's ability and prestige, and this is very bad. No matter how open-minded a leader is, there will always be some discomfort in their heart.
In order to simplify the problem, consider a group with leader . Let the subtree rooted at have nodes. Let be the number of such nodes in this subtree that satisfy . Then person has the probability of of becoming unhappy as a leader (leaders are simple, they are either happy or unhappy). The probabilities for different people are independent.
Now, in order to measure the reasonableness of the company's workplace relationship structure, let us look at some groups. Specifically, we have to answer questions. For question , consider the group led by person . We have to count the expected number of people in this group that will be happy as leaders.
Input
The first line of input contains two integers and (, ).
The next line contains integers , where denotes the superior of person . If , the -th person has no superior, and is the big boss. It is guaranteed that there is exactly one big boss, but it is not guaranteed that the boss is the person number . It is also guaranteed that the superior-subordinate relationships form a rooted tree.
The following line contains integers denoting the ability of each person ().
The subsequent line contains integers denoting the prestige of each person ().
The -th of the following lines contains one integer , asking how many people in the group led by are expected to be happy.
Output
Print lines, with one integer on each: the answers to the queries.
Since the result of a query may not be an integer, you need to output the values modulo .
Formally, it can be shown that the answer can be represented as a fraction for some coprime non-negative integers and . You have to print the value .
Hint
Person will be happy with probability . Person will always be happy.