Gepetto

Time limit: 2.5s Memory limit: 512MB Input: Output:

Grandfather Gepetto's family is very large, consisting of himself and N1N-1 of his descendants (children, grandchildren, great-grandchildren, great-great-great-grandchildren, etc.).

Since Gepetto cannot remember the names of all the members of his family, he has assigned each of them (including himself) a unique number between 11 and NN.

Moreover, since Gepetto doesn't get along well with his relatives by marriage, he only remembers, for each person, who their parent is, that parent also being a descendant of Gepetto. For each 1in1 \le i \le n, let PiP_i be the number associated with the parent of person ii. We consider the number associated with Gepetto's parent to be 00.

On Children's Day, Gepetto wants to give each member of his family (including himself) a gift, but he is torn over the order in which to give out the gifts. An order of giving out the gifts can be represented as a permutation A1,A2,,AnA_1, A_2, \ldots, A_n, meaning that:

  • Gepetto will give the first gift to the person numbered A1A_1;
  • Gepetto will give the second gift to the person numbered A2A_2;
  • \ldots
  • Gepetto will give the last gift to the person numbered AnA_n.

Gepetto considers a gift-giving order to be good if every person in the family receives their gift earlier than all of their children.

After choosing a good gift-giving order V1,V2,VnV_1, V_2, \ldots V_n, Gepetto nevertheless wonders what position this order would occupy in the lexicographically sorted1^1 sequence of all good gift-giving orders.

Since this position can be very large, Gepetto is satisfied with just its remainder modulo 109+710^9+7.

1^1 An order A1,A2,,ANA_1,A_2,\ldots,A_N is lexicographically smaller than another order B1,B2,,BNB_1,B_2,\ldots,B_N if there exists a position ii (1iN1 \le i \le N) such that:

  • Aj=BjA_j=B_j, for all indices jj satisfying 1j<i1 \le j<i;
  • Ai<BiA_i < B_i.

Implementation Details

You must implement the function

int solve(int N, std::vector<int> P, std::vector<int> V)

which receives as parameters:

  • NN, the number of family members.
  • PP, a vector indexed from 00 where PiP_i represents the parent of person i+1i + 1, for every 0i<N0 \leq i < N.
  • VV, a permutation of the numbers {1,2,N}\{1, 2, \ldots N\}, representing the gift-giving order chosen by Gepetto.

The function must return a single integer, representing the index of the permutation in the lexicographically sorted sequence, modulo 1 000 000 0071 \ 000 \ 000 \ 007.

Constraints and Notes

  • 1N1 000 0001 \leq N\leq 1 \ 000 \ 000
  • 0PiN0 \leq P_i \leq N
  • 1ViN1 \leq V_i \leq N
  • It is guaranteed that there exists exactly one ii for which Pi=0P_i=0. This ii is the number associated with Gepetto.
  • It is guaranteed that the graph formed by all the edges (i,Pi)(i, P_i) is a tree (an acyclic undirected graph).
  • It is guaranteed that each number from 11 to NN appears exactly once in the sequence VV. In other words, it is guaranteed that the sequence VV is a permutation of order NN.
# Score Constraints
1 7 1N101 \leq N \leq 10
2 16 1N2001 \leq N \leq 200
3 22 1N5 0001 \leq N \leq 5 \ 000
4 11 There exists a node with N1N-1 children
5 27 1N200 0001 \leq N \leq 200 \ 000
6 17 No additional restrictions.

Example 1

input

6
3 3 0 3 3 3
3 4 6 1 2 5

output

67

Explanation

The family tree from the first example:

Example 2

input

10
4 9 9 0 1 4 9 4 4 8
4 6 1 8 5 10 9 3 7 2

output

5158

Explanation

The family tree from the second example:

Log in or sign up to be able to send submissions!