#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}

const int MaxN = 200005;

vector<int> G[MaxN];
int dep[MaxN], son[MaxN];
int heit[MaxN];
void getInfo(int x){
	heit[x] = son[x] = 0;
	for(int y : G[x]){
		dep[y] = dep[x]+1;
		getInfo(y);
		if(heit[y] > heit[x]){
			heit[x] = heit[y];
			son[x] = y;
		}
	}
	++ heit[x];
}

struct Query{
	int d, id;
};
vector<Query> ask[MaxN];
int ans[MaxN]; // get ans
int tmp[MaxN], *dp[MaxN];
void dfs(int x){
	int *now = dp[x]+heit[x];
	if(son[x]){
		dp[son[x]] = dp[x]+1;
		dfs(son[x]);
	}
	for(int y : G[x])
		if(y != son[x]){
			dp[y] = now; dfs(y);
			for(int j=0; j<heit[y]; ++j)
				dp[x][j+1] += dp[y][j];
			now += heit[y]; // leave them
		}
	dp[x][0] = 1; // itself
	while(!ask[x].empty()){
		int p = ask[x].back().id;
		int v = ask[x].back().d;
		ask[x].pop_back();
		if(v >= dep[x] && v < dep[x]+heit[x])
			ans[p] = dp[x][v-dep[x]];
		else ans[p] = 0;
	}
}

int main(){
	int n = readint();
	for(int i=2,a; i<=n; ++i){
		a = readint();
		G[a].push_back(i);
	}
	int q = readint();
	for(int i=1; i<=q; ++i){
		int x = readint();
		ask[x].push_back(Query());
		ask[x].back().id = i;
		ask[x].back().d = readint();
	}
	getInfo(1); dp[1] = tmp; dfs(1);
	rep(i,1,q) printf("%d\n",ans[i]);
	return 0;
}
#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
const int N=3e3+5,mod=1e9+7;
int t,a[N],pref[N],dp[N][N],n;
vector<int>c[N];
 main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		pref[i] = pref[i-1] + a[i] ;
	}
	dp[0][0] = 1;
	int ans = 0;
	for(int i=1;i<=n;i++) {
		for(int j=0;j<=n;j++) {
			c[pref[j]%i].push_back(j);
		}
		for(int j=0;j<i;j++) {
			int p = 0;
			for(int k=0;k<c[j].size();k++) {
				dp[c[j][k]][i] = p;
				p += dp[c[j][k]][i-1]; 
				if(p>=mod) p-=mod;
			}
			c[j].clear();
		}
		ans += dp[n][i];
		if(ans>=mod) ans -= mod;
	}
	cout<<ans;
}
