JAJJU AND ARROGANCE

0

0 votes
Medium
Problem

Jajju is an arrogant coder and he thinks he can outsmart anyone. Jajju is really good in coding but is very intolerant. So, one day he was roaming somewhere and someone outsmarted him by trapping him in a puzzle. Although he did solve this puzzle but he realized his arrogance and his mistake! Can you answer the puzzle ? Puzzle: You are given N nodes of a tree and each node is assigned a modified fibonacci value. The root is assigned F(K), Kth modified fibonacci number and the root is considered to be at level 0. Every other node is assigned value, F(K+L), where L is the level of the node. Now you will be given Q Queries. The Queries are: You will be given 2 integers, X and K. Where F(K) is the modified fibonacci number assigned to the root. and you have to find the sum of all the nodes in the subtree of X mod 1000000007.

Execute and answer each query to solve the puzzle.

Fibonacci Series:

1 1 5 13 41 ...

F(n)=2xF(n-1)+3xF(n-2), where F(1)=1 and F(2)=1

Input First Line contains N,the number of nodes. Next line contains N-1 space separated integers where ith integer denotes the parent of (i+1)th node. Next line contains a single integer Q,number of queries. Each of the next Q lines contains two integers X and K.

Output Output a single line for each query denoting the answer to the query mod 1000000007.


Input Constraints:
1<=N<=10^5 1<=Q<=50000 1<=K<=10^9 1<=X<=N 1<=P[i+1]<=N (Where P[i+1] is the parent of ith node from i=1 to N-1. Given configuration is always a tree.

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?