#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define popb pop_back()
#define sz size()
#define y1 dfsdsfsdfsd
#define next fdsfsdfjkl
#define int long long
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
double pi = acos(-1.0);
const int inf = 1e9+7;
const ll INF = 1e18+7;
/////////////////////////////////////////

int n;

int a[1000100];
int p[1000100];

vector<int> g[1000100];
int sub[1000100];

void dfs(int v,int pr)
{
    sub[v] = a[v];

    for(int i=0;i<g[v].sz;i++)
    {
        int t = g[v][i];

        if(pr!=t){
            dfs(t,v);
            sub[v] += sub[t];
        }
    }

}

int cnt_sub[1000100];
int cnt[1000100];

int dp[1000100];
main(){speed;
    cin>>n;

    for(int i=1;i<=n;i++) cin>>a[i];

    for(int i=1;i<=n-1;i++)
    {
        cin>>p[i];

        g[p[i]].pb(i+1);
        //g[i+1].pb(p[i]);
    }

    dfs(1,1);

    int sum = sub[1];
    for(int i=1;i<=n;i++)
    {
        int t = sum / __gcd(sum,sub[i]);

        if(t > n) continue;

        cnt_sub[t]++;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j = i;j<=n;j+=i)
        {
            cnt[j] += cnt_sub[i];
        }
    }

    int ans = 0;

    for(int i=n;i>=1;i--)
    {
        if(cnt[i]<i) continue;

        if(cnt[i]==i)
        {
            //cout<<"ok"<<endl;
            dp[i] = 1;

            if(i>1)
                for(int j = i+i;j<=n;j+=i)
                {
                    dp[i] = (dp[j] + dp[i]) % inf;
                }

        }
        ans += dp[i]%inf;
        ans%=inf;
    }

    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define popb pop_back()
#define sz size()
#define y1 dfsdsfsdfsd
#define next fdsfsdfjkl
#define int long long
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
double pi = acos(-1.0);
const int inf = 1e9+7;
const ll INF = 1e18+7;
/////////////////////////////////////////

int n;

int a[1000100];
int p[1000100];

vector<int> g[1000100];
int sub[1000100];

void dfs(int v,int pr)
{
    sub[v] = a[v];

    for(int i=0;i<g[v].sz;i++)
    {
        int t = g[v][i];

        if(pr!=t){
            dfs(t,v);
            sub[v] += sub[t];
        }
    }

}

int cnt_sub[1000100];
int cnt[1000100];

int dp[1000100];
main(){speed;
    cin>>n;

    for(int i=1;i<=n;i++) cin>>a[i];

    for(int i=1;i<=n-1;i++)
    {
        cin>>p[i];

        g[p[i]].pb(i+1);
        g[i+1].pb(p[i]);
    }

    dfs(1,1);

    int sum = sub[1];
    for(int i=1;i<=n;i++)
    {
        int t = sum / __gcd(sum,sub[i]);

        if(t > n) continue;

        cnt_sub[t]++;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j = i;j<=n;j+=i)
        {
            cnt[j] += cnt_sub[i];
        }
    }

    int ans = 0;

    for(int i=n;i>=1;i--)
    {
        if(cnt[i]<i) continue;

        if(cnt[i]==i)
        {
            //cout<<"ok"<<endl;
            dp[i] = 1;

            if(i>1)
                for(int j = i+i;j<=n;j+=i)
                {
                    dp[i] = (dp[j] + dp[i]) % inf;
                }

        }
        ans += dp[i]%inf;
        ans%=inf;
    }

    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;

const int N = (int)1e6 + 50, mod = (int)1e9 + 7;
typedef long long ll;

int n;
int p[N];
ll sum[N], dp[N];
int cnt[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for(int i = 0; i < n; i++) cin >> sum[i];
    for(int i = 1; i < n; i++) cin >> p[i], p[i]--;
    for(int i = n-1; i >= 1; i--) sum[p[i]] += sum[i];
    for(int i = 0; i < n; i++) {
        ll t = sum[0] / __gcd(sum[0], sum[i]);
        if(t <= n) cnt[t] ++;
    }
    for(int i = n; i >= 1; i--) {
        for(int j = 2 * i; j <= n; j += i) cnt[j] += cnt[i];
    }
    for(int i = 1; i <= n; i++) cnt[i] = (cnt[i] == i);
    dp[1] = 1;
    for(int i = 1; i <= n; i++) {
        if(cnt[i] == 0) continue;
        for(int j = 2 * i; j <= n; j += i) {
            if(cnt[j] == 1) dp[j] += dp[i], dp[j] %= mod;
        }
    }
    ll res = 0;
    for(int i = 1; i <= n; i++) res += dp[i], res %= mod;
    cout << res << endl;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define N 2000050
#define mod 1000000007

LL s[N],fa[N],cnt[N],f[N];

int main()
{
	LL n,i,j,ans=0;

    scanf("%lld",&n);
    for (i=1;i<=n;i++) scanf("%lld",&s[i]);
    for (i=2;i<=n;i++) scanf("%lld",&fa[i]);
    for (i=n;i>=2;i--) s[fa[i]]+=s[i];
    for (i=n;i>=1;i--)
    if  ((s[i]=s[1]/__gcd(s[1],s[i]))<=n) cnt[s[i]]++;
    for (i=n;i>=1;i--)
    	for (j=i+i;j<=n;j+=i) cnt[j]+=cnt[i];
    for (f[1]=i=1;i<=n;i++)
    if  (cnt[i]==i)
    {
    	(ans+=f[i])%=mod;
    	for (j=i+i;j<=n;j+=i) (f[j]+=f[i])%=mod;
    }
    printf("%lld\n",ans);

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define int ll
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ls (x<<1)
#define rs (x<<1|1)
#define db double
#define inf 2147483647
#define llinf 9223372036854775807
#define ll long long
#define ull unsigned long long
using namespace std;
typedef pair<int,int> PII;
int inline read(){
    int num=0,neg=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')neg=-1;c=getchar();}
    while(isdigit(c)){num=(num<<3)+(num<<1)+c-'0';c=getchar();}
    return num*neg;
}
const int maxn=1000010,mod=1e9+7;
int n,a[maxn],fa[maxn],s,f[maxn],g[maxn],ans;
int gcd(int x,int y){
	return y?gcd(y,x%y):x;
}
signed main(){
	n=read();for(int i=1;i<=n;i++)a[i]=read(),s+=a[i];
	for(int i=2;i<=n;i++)fa[i]=read();
	for(int i=n;i>=1;i--)a[fa[i]]+=a[i];
	for(int i=1;i<=n;i++)
		if(s/gcd(s,a[i])<=n)f[s/gcd(s,a[i])]++;
//	for(int i=1;i<=n;i++)cout<<f[i]<<" ";cout<<endl;
	for(int i=n;i>=1;i--)
		if(s%i==0){
			for(int j=2*i;j<=n;j+=i)
				if(s%j==0)f[j]=(f[i]+f[j])%mod;
		}
	for(int i=1;i<=n;i++)
		if(s%i==0)f[i]=(f[i]==i);g[1]=1;
	for(int i=1;i<=n;i++)
		if(s%i==0){
			for(int j=2*i;j<=n;j+=i)
				g[j]=(g[j]+f[j]*g[i])%mod;
			ans=(ans+g[i])%mod;
		}
	cout<<ans;return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1,D=1e9+7;
long long n,A,f[N],a[N],p[N],c[N];
int main()
{
	cin>>n,f[1]=1;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=2;i<=n;i++)
		cin>>p[i];
	for(int i=n;i>1;i--)
		a[p[i]]+=a[i];
	for(int i=1;i<=n;i++)
		if(a[1]/__gcd(a[1],a[i])<=n)
			c[a[1]/__gcd(a[1],a[i])]++;
	for(int i=n;i>=1;i--)
		for(int j=i+i;j<=n;j+=i)
			c[j]+=c[i];
	for(int i=1;i<=n;i++)
		if(c[i]==i)
		{
			(A+=f[i])%=D;
			for(int j=i+i;j<=n;j++)
				(f[j]+=f[i])%=D;
		}
	cout<<A;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1,D=1e9+7;
long long n,A,f[N],a[N],p[N],c[N];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n,f[1]=1;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=2;i<=n;i++)
		cin>>p[i];
	for(int i=n;i>1;i--)
		a[p[i]]+=a[i];
	for(int i=1;i<=n;i++)
		if(a[1]/__gcd(a[1],a[i])<=n)
			c[a[1]/__gcd(a[1],a[i])]++;
	for(int i=n;i>=1;i--)
		for(int j=i+i;j<=n;j+=i)
			c[j]+=c[i];
	for(int i=1;i<=n;i++)
		if(c[i]==i)
		{
			(A+=f[i])%=D;
			for(int j=i+i;j<=n;j+=i)
				(f[j]+=f[i])%=D;
		}
	cout<<A;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1,D=1e9+7;
long long n,A,f[N],a[N],p[N],c[N];
int main()
{
	cin>>n,f[1]=1;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=2;i<=n;i++)
		cin>>p[i];
	for(int i=n;i>1;i--)
		a[p[i]]+=a[i];
	for(int i=1;i<=n;i++)
		if(a[1]/__gcd(a[1],a[i])<=n)
			c[a[1]/__gcd(a[1],a[i])]++;
	for(int i=n;i>=1;i--)
		for(int j=i+i;j<=n;j+=i)
			c[j]+=c[i];
	for(int i=1;i<=n;i++)
		if(c[i]==i)
		{
			(A+=f[i])%=D;
			for(int j=i+i;j<=n;j+=i)
				(f[j]+=f[i])%=D;
		}
	cout<<A;
	return 0;
}


// Problem : C. Region Separation
// Contest : Codeforces - Codeforces Round #511 (Div. 1)
// URL : https://codeforces.com/problemset/problem/1034/C
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;

int N;
long long arr[1000005], val[1000005];
vector<int> graph[1000005];
long long cnt[1000005], dp[1000005];
const long long MOD = 1e9+7;
map<long long, long long> mp;

void dfs(int n){
	val[n] = arr[n];
	for(int e : graph[n]){
		dfs(e);
		val[n] += val[e];
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> N;
	long long T = 0;
	for(int i = 1; i<=N; i++){
		cin >> arr[i];
		T += arr[i];
	}
	for(int i = 2; i<=N; i++){
		int p;
		cin >> p;
		graph[p].push_back(i);
	}
	dfs(1);
	for(int i = 1; i<=N; i++){
		long long g = __gcd(val[i], T);
		mp[g]++;
	}
	long long ans = 0;
	dp[1] = 1;
	for(int n = 1; n<=N; n++){
		if(T%n == 0){
			for(auto p : mp){
				if(p.first%(T/n) == 0){
					cnt[n] += p.second;
				}
			}
			//cout << n << " " << cnt[n] << "\n";
			if(cnt[n] == n){
				for(int j = 2*n; j<=N; j+=n){
					dp[j] += dp[n];
					dp[j] %= MOD;
				}
			}
			else{
				dp[n] = 0;
			}
		}
		else{
			dp[n] = 0;
		}
		ans += dp[n];
		ans %= MOD;
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10;
const long long mod = 1e9 + 7;

vector <int> fac[N];
int a[N], p[N], cnt[N];
long long rec[N], dp[N];

long long gcd(long long x, long long y){
	return y == 0 ? x : gcd(y, x % y);
}

int main(){
	int n;
	long long sum, ans, k;
	scanf("%d",&n);
	for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
	for (int i = 2; i <= n; i++) scanf("%d",&p[i]);
	sum = 0;
	for (int i = 1; i <= n; i++) sum += a[i];
	for (int i = n; i; i--){
		rec[i] += a[i];
		a[p[i]] += a[i];
		k = sum / gcd(sum, rec[i]);
		if (k <= n) cnt[k]++;
	}
	for (int i = n; i; i--){
		for (int j = i * 2; j <= n; j += i){
			fac[j].push_back(i);
			cnt[j] += cnt[i];
		}
	}
	ans = 1; dp[1] = 1;
	for (int i = 2; i <= n; i++){
		if (cnt[i] == i){
			for (int j = 0; j < (int)fac[i].size(); j++) dp[i] += dp[fac[i][j]];
			ans += dp[i];
			ans %= mod;
		}
	}
	printf("%I64d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10;
const long long mod = 1e9 + 7;

vector <int> fac[N];
int a[N], p[N], cnt[N];
long long rec[N], dp[N];

long long gcd(long long x, long long y){
	return y == 0 ? x : gcd(y, x % y);
}

int main(){
	int n;
	long long sum, ans, k;
	scanf("%d",&n);
	for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
	for (int i = 2; i <= n; i++) scanf("%d",&p[i]);
	sum = 0;
	for (int i = 1; i <= n; i++) sum += a[i];
	for (int i = n; i; i--){
		rec[i] += a[i];
		rec[p[i]] += rec[i];
		k = sum / gcd(sum, rec[i]);
		if (k <= n) cnt[k]++;
	}
	for (int i = n; i; i--){
		for (int j = i * 2; j <= n; j += i){
			fac[j].push_back(i);
			cnt[j] += cnt[i];
		}
	}
	ans = 1; dp[1] = 1;
	for (int i = 2; i <= n; i++){
		if (cnt[i] == i){
			for (int j = 0; j < (int)fac[i].size(); j++) dp[i] += dp[fac[i][j]];
			ans += dp[i];
			ans %= mod;
		}
	}
	printf("%I64d\n", ans);
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
vector<int> v[nmax];
int ans[nmax],valid[nmax];
long long val[nmax],w[nmax],newval[nmax];
int q[nmax],l[nmax],r[nmax],tt[nmax],d[nmax],s[nmax];
int p,u,source,k,nod,nr;
long long sum;
int n,i,j,x,tot;
void bfs()
{
    for(i=1;i<=n;i++)
        d[i]=l[i]=r[i]=tt[i]=0;
    d[source]=1;
    q[p=u=1]=source;
    for(p=1;p<=u;p++)
    {
        x=q[p];newval[p]=val[x];
        for(i=0;i<v[x].size();i++)
        {
             nod=v[x][i];
             if(!d[nod])
             {
                 q[++u]=nod;
                 d[nod]=d[x]+1;
                 tt[u]=p;
                 if(!l[p])
                    l[p]=u;
                 r[p]=u;
             }
        }
    }
    nr=u;
}
int solve(long long T)
{
    int ret=0;
    for(nod=nr;nod>=1;nod--)
    {
        w[nod]=newval[nod];
        if(w[nod]>T) return 0;
        if(l[nod])
         w[nod]+=(s[l[nod]]-s[r[nod]+1]);
        if(w[nod]>T)
            return 0;
        if(w[nod]==T)
        {
            w[nod]=0;
            ret++;
        }
        s[nod]=s[nod+1]+w[nod];
    }
    return (ret==1LL*sum/T);
}
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>val[i],sum+=val[i];
    for(i=2;i<=n;i++)
    {
        cin>>x;
        v[x].push_back(i);
        v[i].push_back(x);
    }
    source=1;
    bfs();
    for(long long d=1;d<=n;d++)
        if(sum%d==0)
    {
        valid[d]=solve(sum/d);
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=(ans[i]+ans[j])%mod;
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=(tot+ans[i])%mod;
    cout<<(tot+1)%mod;
    return 0;
}


#include <iostream>
#include <fstream>

using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
long long val[nmax];
int valid[nmax],tt[nmax],ans[nmax];
long long sum,x,C;
long long cate[nmax];
int n,i,j,tot;
long long gcd(long long A,long long B)
{
    while(B)
    {
        C=A%B;
        A=B;
        B=C;
    }
    return A;
}
int md(int x)
{
    if(x>=mod)
        x-=mod;
    return x;
}
int main()
{
   // freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>val[i];
        sum+=val[i];
    }
    for(i=2;i<=n;i++)
    {
        cin>>tt[i];
    }
    for(i=n;i>=1;i--)
    {
        val[tt[i]]+=val[i];
        x=gcd(sum,val[i]);
        if(sum/x<=n)
        {
            cate[sum/x]++;
        }
    }
    for(i=n;i>=1;i--)
        for(j=2*i;j<=n;j+=i)
           cate[j]+=cate[i];
    for(long long i=1;i<=n;i++)
        if(sum%i==0)
    {
        if(cate[i]==i)
            valid[i]=1;
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=md(ans[i]+ans[j]);
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=md(tot+ans[i]);
    cout<<(tot+1)%mod;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
vector<int> v[nmax];
int ans[nmax],valid[nmax];
long long val[nmax],w[nmax],newval[nmax];
int q[nmax],l[nmax],r[nmax],tt[nmax],d[nmax];
int p,u,source,k,nod,nr;
long long sum;
int n,i,j,x,tot;
void bfs()
{
    for(i=1;i<=n;i++)
        d[i]=l[i]=r[i]=tt[i]=0;
    d[source]=1;
    q[p=u=1]=source;
    for(p=1;p<=u;p++)
    {
        x=q[p];newval[p]=val[x];
        for(i=0;i<v[x].size();i++)
        {
             nod=v[x][i];
             if(!d[nod])
             {
                 q[++u]=nod;
                 d[nod]=d[x]+1;
                 tt[u]=p;
                 if(!l[p])
                    l[p]=u;
                 r[p]=u;
             }
        }
    }
    nr=u;
}
int solve(long long T)
{
    int ret=0;
    for(nod=nr;nod>=1;nod--)
    {
        w[nod]=newval[nod];
        if(w[nod]>T) return 0;
        for(j=l[nod];j<=r[nod];j++)
            w[nod]+=w[j];
        if(w[nod]>T)
            return 0;
        if(w[nod]==T)
        {
            w[nod]=0;
            ret++;
        }
    }
    return (ret==1LL*sum/T);
}
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>val[i],sum+=val[i];
    for(i=2;i<=n;i++)
    {
        cin>>x;
        v[x].push_back(i);
        v[i].push_back(x);
    }
    source=1;
    bfs();
    for(long long d=1;d<=n;d++)
        if(sum%d==0)
    {
        valid[d]=solve(sum/d);
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=(ans[i]+ans[j])%mod;
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=(tot+ans[i])%mod;
    cout<<(tot+1)%mod;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
vector<int> v[nmax];
int ans[nmax],valid[nmax];
long long val[nmax],w[nmax],newval[nmax],s[nmax];
int q[nmax],l[nmax],r[nmax],tt[nmax],d[nmax];
int p,u,source,k,nod,nr;
long long sum;
int n,i,j,x,tot;
void bfs()
{
    for(i=1;i<=n;i++)
        d[i]=l[i]=r[i]=tt[i]=0;
    d[source]=1;
    q[p=u=1]=source;
    for(p=1;p<=u;p++)
    {
        x=q[p];newval[p]=val[x];
        for(i=0;i<v[x].size();i++)
        {
             nod=v[x][i];
             if(!d[nod])
             {
                 q[++u]=nod;
                 d[nod]=d[x]+1;
                 tt[u]=p;
                 if(!l[p])
                    l[p]=u;
                 r[p]=u;
             }
        }
    }
    nr=u;
}
int solve(long long T)
{
    int ret=0;
    for(nod=nr;nod>=1;nod--)
    {
        w[nod]=newval[nod];
        if(l[nod])
         w[nod]+=(s[l[nod]]-s[r[nod]+1]);
        if(w[nod]>T)
            return 0;
        if(w[nod]==T)
        {
            w[nod]=0;
            ret++;
        }
        s[nod]=s[nod+1]+w[nod];
    }
    return (ret==1LL*sum/T);
}
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>val[i],sum+=val[i];
    for(i=2;i<=n;i++)
    {
        cin>>x;
        v[x].push_back(i);
        v[i].push_back(x);
    }
    source=1;
    bfs();
    for(long long d=1;d<=n;d++)
        if(sum%d==0)
    {
        valid[d]=solve(sum/d);
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=(ans[i]+ans[j])%mod;
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=(tot+ans[i])%mod;
    cout<<(tot+1)%mod;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
vector<int> v[nmax];
int ans[nmax],valid[nmax];
long long val[nmax],w[nmax],newval[nmax];
int q[nmax],l[nmax],r[nmax],tt[nmax],d[nmax];
int p,u,source,k,nod,nr;
long long sum;
int n,i,j,x,tot;
void bfs()
{
    for(i=1;i<=n;i++)
        d[i]=l[i]=r[i]=tt[i]=0;
    d[source]=1;
    q[p=u=1]=source;
    for(p=1;p<=u;p++)
    {
        x=q[p];newval[p]=val[x];
        for(i=0;i<v[x].size();i++)
        {
             nod=v[x][i];
             if(!d[nod])
             {
                 q[++u]=nod;
                 d[nod]=d[x]+1;
                 tt[u]=p;
                 if(!l[p])
                    l[p]=u;
                 r[p]=u;
             }
        }
    }
    nr=u;
}
int solve(long long T)
{
    long long ret=0;
    for(nod=nr;nod>=1;nod--)
    {
        w[nod]=newval[nod];
        if(w[nod]>T) return 0;
        for(j=l[nod];j<=r[nod];j++)
            w[nod]+=w[j];
        if(w[nod]==T)
        {
            w[nod]=0;
            ret++;
        }
    }
    return (ret==sum/T);
}
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>val[i],sum+=val[i];
    for(i=2;i<=n;i++)
    {
        cin>>x;
        v[x].push_back(i);
        v[i].push_back(x);
    }
    source=1;
    bfs();
    for(long long d=1;d<=n;d++)
        if(sum%d==0)
    {
        valid[d]=solve(sum/d);
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=(ans[i]+ans[j])%mod;
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=(tot+ans[i])%mod;
    cout<<(tot+1)%mod;
    return 0;
}


#include <iostream>
#include <fstream>

using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
long long val[nmax];
int valid[nmax],tt[nmax],ans[nmax];
long long sum,x;
long long cate[nmax];
int n,i,j,tot;
long long gcd(long long A,long long B)
{
    if(!B) return A;
    return gcd(B,A%B);
}
int md(int x)
{
    if(x>=mod)
        x-=mod;
}
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>val[i];
        sum+=val[i];
    }
    for(i=2;i<=n;i++)
    {
        cin>>tt[i];
    }
    for(i=n;i>=1;i--)
    {
        val[tt[i]]+=val[i];
        x=gcd(sum,val[i]);
        if(sum/x<=n)
        {
            cate[sum/x]++;
        }
    }
    for(i=n;i>=1;i--)
        for(j=2*i;j<=n;j+=i)
           cate[j]+=cate[i];
    for(long long i=1;i<=n;i++)
        if(sum%i==0)
    {
        if(cate[i]==i)
            valid[i]=1;
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=md(ans[i]+ans[j]);
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=md(tot+ans[i]);
    cout<<(tot+1)%mod;
    return 0;
}


#include <iostream>
#include <fstream>

using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
long long val[nmax];
int valid[nmax],tt[nmax],ans[nmax];
long long sum,x,tot;
long long cate[nmax];
int n,i,j;
long long gcd(long long A,long long B)
{
    if(!B) return A;
    return gcd(B,A%B);
}
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>val[i];
        sum+=val[i];
    }
    for(i=2;i<=n;i++)
    {
        cin>>tt[i];
    }
    for(i=n;i>=1;i--)
    {
        val[tt[i]]+=val[i];
        x=gcd(sum,val[i]);
        if(sum/x<=n)
        {
            cate[sum/x]++;
        }
    }
    for(i=n;i>=1;i--)
        for(j=2*i;j<=n;j+=i)
           cate[j]+=cate[i];
    for(long long i=1;i<=n;i++)
        if(sum%i==0)
    {
        if(cate[i]==i)
            valid[i]=1;
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=(ans[i]+ans[j])%mod;
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=(tot+ans[i])%mod;
    cout<<(tot+1)%mod;
    return 0;
}


#include <iostream>
#include <fstream>

using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
long long val[nmax];
int valid[nmax],tt[nmax],ans[nmax];
long long sum,x;
long long cate[nmax];
int n,i,j,tot;
long long gcd(long long A,long long B)
{
    if(!B) return A;
    return gcd(B,A%B);
}
int md(int x)
{
    if(x>=mod)
        x-=mod;
    return x;
}
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>val[i];
        sum+=val[i];
    }
    for(i=2;i<=n;i++)
    {
        cin>>tt[i];
    }
    for(i=n;i>=1;i--)
    {
        val[tt[i]]+=val[i];
        x=gcd(sum,val[i]);
        if(sum/x<=n)
        {
            cate[sum/x]++;
        }
    }
    for(i=n;i>=1;i--)
        for(j=2*i;j<=n;j+=i)
           cate[j]+=cate[i];
    for(long long i=1;i<=n;i++)
        if(sum%i==0)
    {
        if(cate[i]==i)
            valid[i]=1;
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=md(ans[i]+ans[j]);
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=md(tot+ans[i]);
    cout<<(tot+1)%mod;
    return 0;
}


#include <iostream>
#include <fstream>

using namespace std;
const int nmax=1000*1000+5;
const int mod=1000*1000*1000+7;
long long val[nmax];
int valid[nmax],tt[nmax],ans[nmax];
long long sum,x,tot;
long long cate[nmax];
int n,i,j;
long long gcd(long long A,long long B)
{
    if(!B) return A;
    return gcd(B,A%B);
}
int main()
{
    //freopen("data.in","r",stdin);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>val[i];
        sum+=val[i];
    }
    for(i=2;i<=n;i++)
    {
        cin>>tt[i];
    }
    for(i=n;i>=1;i--)
    {
        val[tt[i]]+=val[i];
        x=gcd(sum,val[i]);
        if(sum/x<=n)
        {
            cate[sum/x]++;
        }
    }
    for(i=n;i>=1;i--)
        for(j=2*i;j<=n;j+=i)
           cate[j]+=cate[i];
    for(long long i=1;i<=n;i++)
        if(sum%i==0)
    {
        if(cate[i]==i)
            valid[i]=1;
    }
    for(i=1;i<=n;i++)
        if(valid[i])
            ans[i]=1;
    for(i=n;i>=1;i--)
       if(valid[i])
        for(j=2*i;j<=n;j+=i)
          ans[i]=(ans[i]+ans[j])%mod;
    for(i=2;i<=n;i++)
      if(valid[i])
        tot=(tot+ans[i])%mod;
    cout<<(tot+1)%mod;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
ll n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
bool vis[maxn];

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  if(x > y) return gcd(y,x);
  return gcd(y%x, x);
}

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int j=2;j*gp <= n;j++) {
    if (cnt[j*gp] == j*gp) {
      ret += solve(j * gp);
      ret %= mod;
    }
  }
  return ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = gcd(psum[i], t);
    ll xx = t /g;
    if (xx<= n) {
      cnt[xx] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }

  ll ret = 0;
  dp[1] ++;
  for(int i=1;i<=n;i++) {
    if (cnt[i] == i) {
      ret += dp[i];
      ret %= mod;
      for(int j=2;j*i<=n;j++) {
        dp[i * j] += dp[i];
        dp[i*j] %= mod;
      }
    }
  }
  cout << ret << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
ll n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
bool vis[maxn];

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int j=2;j*gp <= n;j++) {
    if (cnt[j*gp] == j*gp) {
      ret += solve(j * gp);
      ret %= mod;
    }
  }
  return ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    a[i] = x;
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = __gcd(psum[i], t);
    ll xx = t /g;
    if (xx<= n) {
      cnt[xx] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }

  ll ret = 0;
  dp[1] ++;
  for(int i=1;i<=n;i++) {
    if (cnt[i] == i) {
      ret += dp[i];
      ret %= mod;
      for(int j=2;j*i<=n;j++) {
        dp[i * j] += dp[i];
        dp[i*j] %= mod;
      }
    }
  }
  cout << ret << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
int n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
vector<ll> grps;
bool vis[maxn];

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  if(x > y) return gcd(y,x);
  return gcd(y%x, x);
}

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int j=2;j*gp <= n;j++) {
    if (cnt[j*gp] == j*gp) ret += solve(j * gp);
    ret %= mod;
  }
  return ret;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i] = -1;
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = gcd(psum[i], t);
    if (t / g <= n) {
      cnt[t/g] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }

  for(int i=1;i<=n;i++) if (cnt[i] == i) grps.push_back(i);

  // for(int i=0;i<grps.size();i++) cout << grps[i] << endl;

  ll ret = 1;
  for(int i=2;i<=n;i++) {
    if(cnt[i] == i) {
      ret += solve(i);
      ret %= mod;
    }
  }
  cout << ret << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
ll n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
bool vis[maxn];

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  if(x > y) return gcd(y,x);
  return gcd(y%x, x);
}

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int j=2;j*gp <= n;j++) {
    if (cnt[j*gp] == j*gp) {
      ret += solve(j * gp);
      ret %= mod;
    }
  }
  return ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = gcd(psum[i], t);
    if (t / g <= n) {
      cnt[t/g] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }

  ll ret = 0;
  dp[1] ++;
  for(int i=1;i<=n;i++) {
    if (cnt[i] == i) {
      ret += dp[i];
      ret %= mod;
      for(int j=2;j*i<=n;j++) dp[i * j] += dp[i];
    }
  }
  cout << ret << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
int n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
vector<ll> grps;
bool vis[maxn];

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  if(x > y) return gcd(y,x);
  return gcd(y%x, x);
}

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int j=2;j*gp <= n;j++) {
    if (cnt[j*gp] == j*gp) ret += solve(j * gp);
    ret %= mod
  }
  return ret;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i] = -1;
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = gcd(psum[i], t);
    if (t / g <= n) {
      cnt[t/g] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }

  for(int i=1;i<=n;i++) if (cnt[i] == i) grps.push_back(i);

  // for(int i=0;i<grps.size();i++) cout << grps[i] << endl;

  ll ret = 1;
  for(int i=2;i<=n;i++) {
    if(cnt[i] == i) {
      ret += solve(i);
      ret %= mod;
    }
  }
  cout << ret << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
int n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
vector<ll> grps;
bool vis[maxn];

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  if(x > y) return gcd(y,x);
  return gcd(y%x, x);
}

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int i=0;i<grps.size();i++) {
    if (grps[i] > gp && grps[i] % gp == 0)
      ret += solve(grps[i]);
    ret %= mod;
  }
  return ret;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d" , &x);
    a[i] = x;
    dp[i] = -1;
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = gcd(psum[i], t);
    if (t / g <= n) {
      cnt[t/g] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }

  for(int i=1;i<=n;i++) if (cnt[i] == i) grps.push_back(i);

  // for(int i=0;i<grps.size();i++) cout << grps[i] << endl;

  ll ret = 1;
  for(int i=2;i<=n;i++) {
    if(cnt[i] == i) {
      ret += solve(i);
      ret %= mod;
    }
  }
  cout << ret << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
ll n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
bool vis[maxn];

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  if(x > y) return gcd(y,x);
  return gcd(y%x, x);
}

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int j=2;j*gp <= n;j++) {
    if (cnt[j*gp] == j*gp) ret += solve(j * gp);
    ret %= mod;
  }
  return ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i] = -1;
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = gcd(psum[i], t);
    if (t / g <= n {
      cnt[t/g] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }
  cout << solve(1) << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
ll n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
bool vis[maxn];

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  if(x > y) return gcd(y,x);
  return gcd(y%x, x);
}

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int j=2;j*gp <= n;j++) {
    if (cnt[j*gp] == j*gp) ret += solve(j * gp);
    ret %= mod;
  }
  return ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i] = -1;
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = gcd(psum[i], t);
    if (t / g <= n) {
      cnt[t/g] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }
  cout << solve(1) << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 7;

ll a[maxn];
vector<int> adj[maxn];
int n;

ll psum[maxn];
ll cnt[maxn];
ll dp[maxn];
vector<ll> grps;
bool vis[maxn];

ll gcd(ll x, ll y) {
  if (x == 0) return y;
  if(x > y) return gcd(y,x);
  return gcd(y%x, x);
}

ll dfs(int here) {
  vis[here] = true;
  ll ret = a[here];
  for (int i = 0; i < adj[here].size(); i++) {
    int there = adj[here][i];
    if (vis[there]) continue;
    ret += dfs(there);
  }
  psum[here] = ret;
  return ret;
}

ll solve(int gp){
  ll& ret = dp[gp];
  if (ret!=-1) return ret;
  ret = 1;
  for(int i=0;i<grps.size();i++) {
    if (grps[i] > gp && grps[i] % gp == 0)
      ret += solve(grps[i]);
    ret %= mod;
  }
  return ret;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    dp[i] = -1;
  }

  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adj[i].push_back(x);
    adj[x].push_back(i);
  }

  dfs(1);
  ll t = psum[1];

  for(int i=1;i<=n;i++) {
    ll g = gcd(psum[i], t);
    if (t / g <= n) {
      cnt[t/g] ++;
    }
  }

  for(int i=n;i>=1;i--) {
    for(int j=2;i*j<=n;j++) cnt[i*j] += cnt[i];
  }

  for(int i=1;i<=n;i++) if (cnt[i] == i) grps.push_back(i);

  // for(int i=0;i<grps.size();i++) cout << grps[i] << endl;

  ll ret = 1;
  for(int i=2;i<=n;i++) {
    if(cnt[i] == i) {
      ret += solve(i);
      ret %= mod;
    }
  }
  cout << ret << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
#define ll long long
const int mod=1e9+7;
int p[maxn],num[maxn],num2[maxn];
ll s[maxn],dp[maxn];
bool ok[maxn];
main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lld",&s[i]);
    for(int i=2;i<=n;i++)scanf("%d",&p[i]);
    for(int i=n;i>1;i--)s[p[i]]+=s[i];
    for(int i=1;i<=n;i++)
    {
        ll tmp=s[1]/__gcd(s[1],s[i]);
        if(tmp<=n)num[tmp]++;
    }
    for(int i=1;i<=n;i++)if(num[i])
        for(int j=i;j<=n;j+=i)num2[j]+=num[i];
    for(int i=1;i<=n;i++)ok[i]=(num2[i]>=i);
    //for(int i=1;i<=n;i++)if(ok[i])printf("%d ",i);puts("");
    dp[1]=1;
    for(int i=1;i<=n;i++)if(ok[i])
    {
        for(int j=2*i;j<=n;j+=i)if(ok[j])
            (dp[j]+=dp[i])%=mod;
    }
    ll ans=0;
    for(int i=1;i<=n;i++)if(ok[i])(ans+=dp[i])%=mod;
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1000010
#define ll long long
const int mod=1e9+7;
int p[maxn],num[maxn],num2[maxn];
ll s[maxn],dp[maxn];
bool ok[maxn];
main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lld",&s[i]);
    for(int i=2;i<=n;i++)scanf("%d",&p[i]);
    for(int i=n;i>1;i--)s[p[i]]+=s[i];
    for(int i=1;i<=n;i++)
    {
        ll tmp=s[1]/__gcd(s[1],s[i]);
        if(tmp<=n)num[tmp]++;
    }
    for(int i=1;i<=n;i++)if(num[i])
        for(int j=i;j<=n;j+=i)num2[j]+=num[i];
    for(int i=1;i<=n;i++)ok[i]=(num2[i]>=i);
    //for(int i=1;i<=n;i++)if(ok[i])printf("%d ",i);puts("");
    dp[1]=1;
    for(int i=1;i<=n;i++)if(ok[i])
    {
        for(int j=2*i;j<=n;j+=i)if(ok[j])
            (dp[j]+=dp[i])%=mod;
    }
    ll ans=0;
    for(int i=1;i<=n;i++)if(ok[i])(ans+=dp[i])%=mod;
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
#define ll long long
const int mod=1e9+7;
int p[maxn],num[maxn];
ll s[maxn],dp[maxn];
bool ok[maxn];
main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lld",&s[i]);
    for(int i=2;i<=n;i++)scanf("%d",&p[i]);
    for(int i=n;i>1;i--)s[p[i]]+=s[i];
    for(int i=n;i>=1;i--)s[i]=s[1]/__gcd(s[1],s[i]);
    for(int i=1;i<=n;i++)
        if(s[i]<=n)num[s[i]]++;
    for(int i=1;i<=n;i++)
        for(int j=2*i;j<=n;j+=i)num[j]+=num[i];
    for(int i=1;i<=n;i++)ok[i]=(num[i]>=i);
    //for(int i=1;i<=n;i++)if(ok[i])printf("%d ",i);puts("");
    dp[1]=1;
    for(int i=1;i<=n;i++)if(ok[i])
    {
        for(int j=2*i;j<=n;j+=i)if(ok[j])
            (dp[j]+=dp[i])%=mod;
    }
    ll ans=0;
    for(int i=1;i<=n;i++)if(ok[i])(ans+=dp[i])%=mod;
    printf("%lld\n",ans);
    return 0;
}


#include <bits/stdc++.h>
const int N=1000005,mu=1e9+7;
int n,f[N],ans,tag[N];
long long s[N],sum;
void reduce(int &x){ x+=x>>31μ }
long long gcd(long long x,long long y){
	if (x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&s[i]),sum+=s[i];
	for (int i=2;i<=n;i++) scanf("%d",&f[i]);
	for (int i=n;i>=1;i--) s[f[i]]+=s[i];
	memset(f,0,sizeof(f));
	for (int i=1;i<=n;i++)
		if (sum/gcd(s[i],sum)<=n)
			f[sum/gcd(s[i],sum)]++;
	for (int i=n;i>=1;i--)
		if (f[i]) for (int j=i+i;j<=n;j+=i) f[j]+=f[i];

	for (int i=1;i<=n;i++) f[i]=(f[i]==i),tag[i]=f[i];
	for (int i=2;i<=n;i++){
		for (int j=i+i;j<=n;j++) reduce(f[j]+=f[i]-mu);
		if (tag[i] && sum%i==0) reduce(ans+=f[i]-mu);
	}
	printf("%d\n",ans+1);
}


#include <bits/stdc++.h>
const int N=1000005,mu=1e9+7;
int n,s[N],f[N],ans,sum,tag[N];
void reduce(int &x){ x+=x>>31μ }
int gcd(int x,int y){
	if (x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&s[i]),sum+=s[i];
	for (int i=2;i<=n;i++) scanf("%d",&f[i]);
	for (int i=n;i>=1;i--) s[f[i]]+=s[i];
	memset(f,0,sizeof(f));
	for (int i=1;i<=n;i++)
		if (sum/gcd(s[i],sum)<=n) f[sum/gcd(s[i],sum)]++;
	for (int i=n;i>=1;i--)
		if (f[i]) for (int j=i+i;j<=n;j+=i) f[j]+=f[i];

	for (int i=1;i<=n;i++) f[i]=(f[i]==i),tag[i]=f[i];
	for (int i=2;i<=n;i++){
		if (!tag[i]) continue;
		for (int j=i+i;j<=n;j++) reduce(f[j]+=f[i]-mu);
		if (sum%i==0) reduce(ans+=f[i]-mu);
	}
	printf("%d\n",ans+1);
}


#include <bits/stdc++.h>
const int N=1000005,mu=1e9+7;
int n,f[N],tag[N],num[N],ans[N],Ans;
long long s[N],sum;
void reduce(int &x){ x+=x>>31μ }
long long gcd(long long x,long long y){
	if (x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&s[i]),sum+=s[i];
	for (int i=2;i<=n;i++) scanf("%d",&f[i]);
	for (int i=n;i>=1;i--) s[f[i]]+=s[i];
	memset(f,0,sizeof(f));
	for (int i=1;i<=n;i++)
		if (sum/gcd(s[i],sum)<=n)
			f[sum/gcd(s[i],sum)]++;
	for (int i=n;i>=1;i--)
		if (f[i]) for (int j=i+i;j<=n;j+=i) f[j]+=f[i];
	for (int i=1;i<=n;i++) f[i]=(f[i]==i);
	/*for (int i=2;i<=n;i++){
		if (!tag[i]) continue;
		for (int j=i+i;j<=n;j++) reduce(f[j]+=f[i]-mu);
		if (tag[i] && sum%i==0) reduce(ans+=f[i]-mu);
	}*/
   for(int i=ans[1]=1;i<=n;i++) if(f[i])
		for(int j=i*2;j<=n;j+=i) reduce(ans[j]+=ans[i]-mu);
	for(int i=1;i<=n;i++) if(f[i]) reduce(Ans+=ans[i]-mu);
	printf("%d\n",Ans);
}


#include <bits/stdc++.h>
const int N=1000005,mu=1e9+7;
int n,f[N],tag[N],num[N],ans;
long long s[N],sum;
void reduce(int &x){ x+=x>>31μ }
long long gcd(long long x,long long y){
	if (x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&s[i]),sum+=s[i];
	for (int i=2;i<=n;i++) scanf("%d",&f[i]);
	for (int i=n;i>=1;i--) s[f[i]]+=s[i];
	memset(f,0,sizeof(f));
	for (int i=1;i<=n;i++)
		if (sum/gcd(s[i],sum)<=n)
			f[sum/gcd(s[i],sum)]++;
	for (int i=n;i>=1;i--)
		if (f[i]) for (int j=i+i;j<=n;j+=i) f[j]+=f[i];
	for (int i=1;i<=n;i++) f[i]=(f[i]==i),tag[i]=f[i];
	for (int i=2;i<=n;i++){
		if (!tag[i]) continue;
		for (int j=i+i;j<=n;j+=i) reduce(f[j]+=f[i]-mu);
		if (tag[i] && sum%i==0) reduce(ans+=f[i]-mu);
	}
	printf("%d\n",ans+1);
}


#include <bits/stdc++.h>
const int N=1000005,mu=1e9+7;
int n,f[N],ans,tag[N];
long long s[N],sum;
void reduce(int &x){ x+=x>>31μ }
long long gcd(long long x,long long y){
	if (x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&s[i]),sum+=s[i];
	for (int i=2;i<=n;i++) scanf("%d",&f[i]);
	for (int i=n;i>=1;i--) s[f[i]]+=s[i];
	memset(f,0,sizeof(f));
	for (int i=1;i<=n;i++)
		if (sum/gcd(s[i],sum)<=n)
			f[sum/gcd(s[i],sum)]++;
	for (int i=n;i>=1;i--)
		if (f[i]) for (int j=i+i;j<=n;j+=i) f[j]+=f[i];

	for (int i=1;i<=n;i++) f[i]=(f[i]==i),tag[i]=f[i];
	for (int i=2;i<=n;i++){
		if (!tag[i]) continue;
		for (int j=i+i;j<=n;j++) reduce(f[j]+=f[i]-mu);
		if (sum%i==0) reduce(ans+=f[i]-mu);
	}
	printf("%d\n",ans+1);
}


#include <bits/stdc++.h>
const int N=1000005,mu=1e9+7;
int n,f[N],ans,tag[N],num[N];
long long s[N],sum;
void reduce(int &x){ x+=x>>31μ }
long long gcd(long long x,long long y){
	if (x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&s[i]),sum+=s[i];
	for (int i=2;i<=n;i++) scanf("%d",&f[i]);
	for (int i=n;i>=1;i--) s[f[i]]+=s[i];
	memset(f,0,sizeof(f));
	/*for (int i=1;i<=n;i++)
		if (sum/gcd(s[i],sum)<=n)
			f[sum/gcd(s[i],sum)]++;
	for (int i=n;i>=1;i--)
		if (f[i]) for (int j=i+i;j<=n;j+=i) f[j]+=f[i];

	for (int i=1;i<=n;i++) f[i]=(f[i]==i),tag[i]=f[i];
	for (int i=2;i<=n;i++){
		if (!tag[i]) continue;
		for (int j=i+i;j<=n;j++) reduce(f[j]+=f[i]-mu);
		if (tag[i] && sum%i==0) reduce(ans+=f[i]-mu);
	}*/
	for( int i = 1 ; i <= n ; ++ i ) if( sum / gcd( s[i] , sum ) <= n ) ++ num[sum / gcd( s[i] , sum )];
    for( int i = 1 ; i <= n ; ++ i ) if( num[i] )
        for( int j = i ; j <= n ; j += i )
            f[j] += num[i];
    for( int i = 1 ; i <= n ; ++ i ) f[i] = ( f[i] == i );
    for( int i = n ; i > 1 ; -- i ) if( f[i] ) {
        for (int j = i + i; j <= n; j += i) ( f[i] += f[j] ) %= mu;
        ( ans += f[i] ) %= mu;
    }
	printf("%d\n",ans+1);
}


#include <bits/stdc++.h>
const int N=1000005,mu=1e9+7;
int n,f[N],ans,tag[N];
long long s[N],sum;
void reduce(int &x){ x+=x>>31μ }
long long gcd(int x,int y){
	if (x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&s[i]),sum+=s[i];
	for (int i=2;i<=n;i++) scanf("%d",&f[i]);
	for (int i=n;i>=1;i--) s[f[i]]+=s[i];
	memset(f,0,sizeof(f));
	for (int i=1;i<=n;i++)
		if (sum/gcd(s[i],sum)<=n) f[sum/gcd(s[i],sum)]++;
	for (int i=n;i>=1;i--)
		if (f[i]) for (int j=i+i;j<=n;j+=i) f[j]+=f[i];

	for (int i=1;i<=n;i++) f[i]=(f[i]==i),tag[i]=f[i];
	for (int i=2;i<=n;i++){
		if (!tag[i]) continue;
		for (int j=i+i;j<=n;j++) reduce(f[j]+=f[i]-mu);
		if (sum%i==0) reduce(ans+=f[i]-mu);
	}
	printf("%d\n",ans+1);
}


#include <bits/stdc++.h>
const int N=1000005,mu=1e9+7;
int n,f[N],ans,tag[N],num[N];
long long s[N],sum;
void reduce(int &x){ x+=x>>31μ }
long long gcd(long long x,long long y){
	if (x%y==0) return y;
	return gcd(y,x%y);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&s[i]),sum+=s[i];
	for (int i=2;i<=n;i++) scanf("%d",&f[i]);
	for (int i=n;i>=1;i--) s[f[i]]+=s[i];
	memset(f,0,sizeof(f));
	for (int i=1;i<=n;i++)
		if (sum/gcd(s[i],sum)<=n)
			f[sum/gcd(s[i],sum)]++;
	for (int i=n;i>=1;i--)
		if (f[i]) for (int j=i+i;j<=n;j+=i) f[j]+=f[i];

	/*for (int i=1;i<=n;i++) f[i]=(f[i]==i),tag[i]=f[i];
	for (int i=2;i<=n;i++){
		if (!tag[i]) continue;
		for (int j=i+i;j<=n;j++) reduce(f[j]+=f[i]-mu);
		if (tag[i] && sum%i==0) reduce(ans+=f[i]-mu);
	}*/
    for( int i = 1 ; i <= n ; ++ i ) f[i] = ( f[i] == i );
    for( int i = n ; i > 1 ; -- i ) if( f[i] ) {
        for (int j = i + i; j <= n; j += i) ( f[i] += f[j] ) %= mu;
        ( ans += f[i] ) %= mu;
    }
	printf("%d\n",ans+1);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
#define mp make_pair
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void wri(ll a){write(a); putchar(' ');}
inline void writeln(ll a){write(a); puts("");}
const int N=1000005,mod=1000000007;
int n,ans,a[N],p[N],f[N],dp[N];
ll sz[N],q[N];
int find(ll x){
	return upper_bound(&q[1],&q[n+1],x)-lower_bound(&q[1],&q[n+1],x);
}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=2;i<=n;i++)p[i]=read();
	for(int i=n;i;i--){
		sz[i]+=a[i]; sz[p[i]]+=sz[i]; q[i]=sz[i];
	}
	sort(&q[1],&q[n+1]);
	for(int i=1;i<=n;i++)if(sz[1]%i==0){
		ll t=sz[1]/i; int dq=0;
		for(int j=1;j<=i;j++){
			dq+=find(t*j); if(dq<j)break;
		}
		if(dq==i)f[i]=1;
	}
	dp[1]=1;
	for(int i=1;i<=n;i++)if(f[i]){
		for(int j=i+i;j<=n;j+=i)dp[j]=(dp[j]+dp[i])%mod;
		ans=(ans+dp[i])%mod;
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
#define mp make_pair
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void wri(ll a){write(a); putchar(' ');}
inline void writeln(ll a){write(a); puts("");}
const int N=1000005,mod=1000000007;
int n,ans,a[N],p[N],f[N],dp[N];
ll sz[N];
map<ll,int> M;
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=2;i<=n;i++)p[i]=read();
	for(int i=n;i;i--){
		sz[i]+=a[i]; sz[p[i]]+=sz[i]; M[sz[i]]++;
	}
	for(int i=1;i<=n;i++)if(sz[1]%i==0){
		ll t=sz[1]/i; int dq=0;
		for(int j=1;j<=i;j++)if(M.count(t*j)){
			dq+=M[t*j]; if(dq<j)break;
		}
		if(dq==i)f[i]=1;
	}
	dp[1]=1;
	for(int i=1;i<=n;i++)if(f[i]){
		for(int j=i+i;j<=n;j+=i)dp[j]=(dp[j]+dp[i])%mod;
		ans=(ans+dp[i])%mod;
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
#define mp make_pair
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void wri(ll a){write(a); putchar(' ');}
inline void writeln(ll a){write(a); puts("");}
const int N=1000005,mod=1000000007;
int n,ans,a[N],p[N],f[N],dp[N];
ll sz[N];
#define P 3000007
struct hash_table{
	ll a[P]; int b[P];
	bool hash[P];
	void insert(ll x){
		x=x*x;
		for(int i=x%P;;i=i+1==P?0:i+1){
			if(!hash[i]||a[i]==x){
				hash[i]=1; a[i]=x; b[i]++; return;
			}
		}
	}
	int find(ll x){
		x=x*x;
		for(int i=x%P;;i=i+1==P?0:i+1){
			if(!hash[i]||a[i]==x)return b[i];
		}
	}
}M;
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=2;i<=n;i++)p[i]=read();
	for(int i=n;i;i--){
		sz[i]+=a[i]; sz[p[i]]+=sz[i]; M.insert(sz[i]);
	}
	for(int i=1;i<=n;i++)if(sz[1]%i==0){
		ll t=sz[1]/i; int dq=0;
		for(int j=1;j<=i;j++){
			dq+=M.find(t*j); if(dq<j)break;
		}
		if(dq==i)f[i]=1;
	}
	dp[1]=1;
	for(int i=1;i<=n;i++)if(f[i]){
		for(int j=i+i;j<=n;j+=i)dp[j]=(dp[j]+dp[i])%mod;
		ans=(ans+dp[i])%mod;
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
#define mp make_pair
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void wri(ll a){write(a); putchar(' ');}
inline void writeln(ll a){write(a); puts("");}
const int N=1000005,mod=1000000007;
int n,ans,a[N],p[N],f[N],dp[N];
ll sz[N];
#define P 3000007
struct hash_table{
	ll a[P]; int b[P];
	bool hash[P];
	void insert(ll x){
		x=x*x; if(x<0)x=-x;
		for(int i=x%P;;i=i+1==P?0:i+1){
			if(!hash[i]||a[i]==x){
				hash[i]=1; a[i]=x; b[i]++; return;
			}
		}
	}
	int find(ll x){
		x=x*x; if(x<0)x=-x;
		for(int i=x%P;;i=i+1==P?0:i+1){
			if(!hash[i]||a[i]==x)return b[i];
		}
	}
}M;
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=2;i<=n;i++)p[i]=read();
	for(int i=n;i;i--){
		sz[i]+=a[i]; sz[p[i]]+=sz[i]; M.insert(sz[i]);
	}
	for(int i=1;i<=n;i++)if(sz[1]%i==0){
		ll t=sz[1]/i; int dq=0;
		for(int j=1;j<=i;j++){
			dq+=M.find(t*j); if(dq<j)break;
		}
		if(dq==i)f[i]=1;
	}
	dp[1]=1;
	for(int i=1;i<=n;i++)if(f[i]){
		for(int j=i+i;j<=n;j+=i)dp[j]=(dp[j]+dp[i])%mod;
		ans=(ans+dp[i])%mod;
	}
	cout<<ans<<endl;
}


#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cstdlib>

#include <algorithm>
#include <numeric>
#include <limits>
#include <functional>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <queue>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 1000010, P = 1000000007;

int n;
int p[N], cnt[N];
ll a[N];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
#ifdef LBT
//	freopen("test.in", "r", stdin);
	int nol_cl = clock();
#endif

	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
		scanf("%lld", &a[i]);
	for (int i = 2; i <= n; ++i)
		scanf("%d", &p[i]);
	for (int i = n; i; --i)
		a[p[i]] += a[i];
	for (int i = 1; i <= n; ++i) {
		ll g = a[1] / gcd(a[1], a[i]);
		if (g <= n)
			++cnt[g];
	}
	for (int i = n; i; --i)
		for (int j = i * 2; j <= n; j += i)
			cnt[j] += cnt[i];
	for (int i = n; i; --i) {
		if (cnt[i] != i) {
			cnt[i] = 0;
			continue;
		}
		cnt[i] = 1;
		for (int j = i * 2; j <= n; j += i)
			if ((cnt[i] += cnt[j]) >= P)
				cnt[i] -= P;
	}
	printf("%d\n", cnt[1]);

#ifdef LBT
	LOG("Time: %dms\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define pii pair<ll,ll>
#define x1 dlfjl
#define x2 dkfj
#define x3 dflgkg
#define y1 dskgj
#define y2 dskfj
#define y3 dfhgdh
#define SQ 320
#define PI 3.14159265359
#define MOD 1000000007
#define ROW 5
typedef long long ll;
using namespace std;
ll n,i,j,k,m,q,timer,mx, v,tot,x,y,INF, sz,ans, sum;
ll a[1005000], dp[1005000], b[1050000];
vector<int> g[1005000];
vector<ll> divs;
map<ll, ll> total_dp, cnt, cool, tot_cnt;
const int N = 32000000;
int lp[N+15];
vector<int> pr;
vector<ll> cur_primes, all, good;

ll gcd(ll a, ll b) {
    return b==0?a:gcd(b, a%b);
}

void dfs(int v, int p = -1) {
    dp[v] = a[v];
    for (int i = 0; i < g[v].size(); i++) {
        int to = g[v][i];
        if (to != p) {
            dfs(to, v);
            dp[v] += dp[to];
        }
    }
    /*if (sum % dp[v] == 0 && total_dp.find(dp[v]) == total_dp.end()) {
        total_dp[dp[v]] = 0;
        all.push_back(dp[v]);
    }*/
    cnt[gcd(dp[v], sum)]++;
}

void go(ll v, ll pos) {
    for (int i = pos; i < cur_primes.size(); i++) {
        ll u = cur_primes[i];
        if (v % u == 0) {
            go(v / u, i);
        }
    }
    all.push_back(v);
}

int main() {
    //freopen("input.txt", "r", stdin);
    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
            lp[i * pr[j]] = pr[j];
    }
    cin >> n;
    for (int i = 1; i <= n; i++) {
        scanf("%I64d", &a[i]);
        sum += a[i];
    }
    for (int i = 2; i <= n; i++) {
        int x, y;
        scanf("%d", &x);
        g[i].push_back(x);
        g[x].push_back(i);
    }
    //cout << "hyu" << endl;
    dfs(1);
    ll x = sum;
    for (int i = 0; i < pr.size(); i++) {
        if (x % pr[i] == 0) {
            cur_primes.push_back(pr[i]);
            while (x % pr[i] == 0)
                x /= pr[i];
        }
    }
    if (x != 1)
        cur_primes.push_back(x);
    go(sum, 0);
    //for (int i = 0; i < all.size(); i++)
    //    cout << all[i] << endl;
    sort(all.begin(), all.end());
    reverse(all. begin(), all.end());
    for (int j = 0; j < all.size(); j++) {
        sz = 0;
        ll v = all[j];
        for (int i = 0; i < cur_primes.size(); i++)
        if (sum % (v * cur_primes[i]) == 0) {
            b[sz++] = cur_primes[i];
        }
        ll ans = 0;
        for (int i = 1; i < (1<<sz); i++) {
            int bits = 0;
            ll mul = 1;
            for (int k = 0; k < sz; k++)
            if ((i>>k)&1) {
                bits++;
                mul *= b[k];
            }
            if (bits % 2)
                ans += tot_cnt[v*mul];
            else
                ans -= tot_cnt[v*mul];
        }
        tot_cnt[v] = cnt[v] + ans;
        //cout << v << " " << tot_cnt[v] << " " << cnt[v] << endl;
    }
    for (int i = 0; i < all.size(); i++) {
        ll v = all[i];
        if (sum / v == tot_cnt[v]) {
            total_dp[v] = 1, cool[v] = 1;
            good.push_back(v);
        }
    }
    sort(good.begin(), good.end());
    for (int i = 0; i < good.size(); i++) {
        for (int j = i+1; j < good.size(); j++)
            if (good[j] % good[i] == 0) {
                total_dp[good[j]] += total_dp[good[i]];
                if (total_dp[good[j]] >= MOD)
                    total_dp[good[j]] -= MOD;
            }
    }
    cout << total_dp[sum] << endl;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define pii pair<ll,ll>
#define x1 dlfjl
#define x2 dkfj
#define x3 dflgkg
#define y1 dskgj
#define y2 dskfj
#define y3 dfhgdh
#define SQ 320
#define PI 3.14159265359
#define MOD 1000000007
#define ROW 5
typedef long long ll;
using namespace std;
ll n,i,j,k,m,q,timer,mx, v,tot,x,y,INF, sz,ans, sum;
ll a[1005000], dp[1005000], b[105];
vector<int> g[1005000];
const int N = 32000000;
int lp[N+15];
vector<int> pr;
vector<ll> cur_primes, all, good;

struct Hashset {
    long long keys[300000];
    int values[300000];
    void update(ll key, int value) {
        int hsh = (key&262143);
        while (keys[hsh] != -1 && keys[hsh] != key) {
            hsh++;
        }
        //cout << hsh << " " << value << endl;
        if (keys[hsh] == -1)
            keys[hsh] = key, values[hsh] = value;
        else {
            values[hsh] += value;
            if (values[hsh] >= MOD)
                values[hsh] -= MOD;
        }
    }
    int get(ll key) {
        int hsh = (key&262143);
        while (keys[hsh] != -1 && keys[hsh] != key) {
            hsh++;
        }
        if (keys[hsh] == -1)
            return 0;
        return values[hsh];
    }
} cnt, tot_cnt, total_dp;

ll gcd(ll a, ll b) {
    return b==0?a:gcd(b, a%b);
}

void dfs(int v, int p = -1) {
    dp[v] = a[v];
    for (int i = 0; i < g[v].size(); i++) {
        int to = g[v][i];
        if (to != p) {
            dfs(to, v);
            dp[v] += dp[to];
        }
    }
    /*if (sum % dp[v] == 0 && total_dp.find(dp[v]) == total_dp.end()) {
        total_dp[dp[v]] = 0;
        all.push_back(dp[v]);
    }*/
    cnt.update(gcd(dp[v], sum), 1);
}

void go(ll v, ll pos) {
    for (int i = pos; i < cur_primes.size(); i++) {
        ll u = cur_primes[i];
        if (v % u == 0) {
            go(v / u, i);
        }
    }
    all.push_back(v);
}

int main() {
    //freopen("input.txt", "r", stdin);
    for (int i = 0; i < 300000; i++)
        cnt.keys[i] = tot_cnt.keys[i] = total_dp.keys[i] = -1;
    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
            lp[i * pr[j]] = pr[j];
    }
    cin >> n;
    for (int i = 1; i <= n; i++) {
        scanf("%I64d", &a[i]);
        sum += a[i];
    }
    for (int i = 2; i <= n; i++) {
        int x, y;
        scanf("%d", &x);
        g[i].push_back(x);
        g[x].push_back(i);
    }
    //cout << "hyu" << endl;
    dfs(1);
    ll x = sum;
    for (int i = 0; i < pr.size(); i++) {
        if (x % pr[i] == 0) {
            cur_primes.push_back(pr[i]);
            while (x % pr[i] == 0)
                x /= pr[i];
        }
    }
    if (x != 1)
        cur_primes.push_back(x);
    go(sum, 0);
    //for (int i = 0; i < all.size(); i++)
    //    cout << all[i] << endl;
    sort(all.begin(), all.end());
    reverse(all. begin(), all.end());
    for (int j = 0; j < all.size(); j++) {
        sz = 0;
        ll v = all[j];
        for (int i = 0; i < cur_primes.size(); i++)
        if (sum % (v * cur_primes[i]) == 0) {
            b[sz++] = cur_primes[i];
        }
        ll ans = 0;
        for (int i = 1; i < (1<<sz); i++) {
            int bits = 0;
            ll mul = 1;
            for (int k = 0; k < sz; k++)
            if ((i>>k)&1) {
                bits++;
                mul *= b[k];
            }
            if (bits % 2)
                ans += tot_cnt.get(v*mul);
            else
                ans -= tot_cnt.get(v*mul);
        }
        tot_cnt.update(v, cnt.get(v) + ans);
        //cout << v << " " << tot_cnt[v] << " " << cnt[v] << endl;
    }
    for (int i = 0; i < all.size(); i++) {
        ll v = all[i];
        if (sum / v == tot_cnt.get(v)) {
            total_dp.update(v, 1);
            good.push_back(v);
        }
    }
    sort(good.begin(), good.end());
    for (int i = 0; i < good.size(); i++) {
        for (int j = i+1; j < good.size(); j++)
            if (good[j] % good[i] == 0) {
                total_dp.update(good[j], total_dp.get(good[i]));
            }
    }
    cout << total_dp.get(sum) << endl;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define pii pair<ll,ll>
#define x1 dlfjl
#define x2 dkfj
#define x3 dflgkg
#define y1 dskgj
#define y2 dskfj
#define y3 dfhgdh
#define SQ 320
#define PI 3.14159265359
#define MOD 1000000007
#define ROW 5
typedef long long ll;
using namespace std;
ll n,i,j,k,m,q,timer,mx, v,tot,x,y,INF, sz,ans, sum;
ll a[1005000], dp[1005000], b[1050000];
vector<int> g[1005000];
vector<ll> divs;
const int N = 32000000;
int lp[N+15];
vector<int> pr;
vector<ll> cur_primes, all, good;

struct Hashset {
    long long keys[1100000];
    int values[1100000];
    void update(ll key, int value) {
        int hsh = (key&1048575);
        while (keys[hsh] != -1 && keys[hsh] != key) {
            hsh++;
        }
        //cout << hsh << " " << value << endl;
        if (keys[hsh] == -1)
            keys[hsh] = key, values[hsh] = value;
        else {
            values[hsh] += value;
            if (values[hsh] >= MOD)
                values[hsh] -= MOD;
        }
    }
    int get(ll key) {
        int hsh = (key&1048575);
        while (keys[hsh] != -1 && keys[hsh] != key) {
            hsh++;
        }
        if (keys[hsh] == -1)
            return 0;
        return values[hsh];
    }
} cnt, tot_cnt, total_dp;

ll gcd(ll a, ll b) {
    return b==0?a:gcd(b, a%b);
}

void dfs(int v, int p = -1) {
    dp[v] = a[v];
    for (int i = 0; i < g[v].size(); i++) {
        int to = g[v][i];
        if (to != p) {
            dfs(to, v);
            dp[v] += dp[to];
        }
    }
    /*if (sum % dp[v] == 0 && total_dp.find(dp[v]) == total_dp.end()) {
        total_dp[dp[v]] = 0;
        all.push_back(dp[v]);
    }*/
    cnt.update(gcd(dp[v], sum), 1);
}

void go(ll v, ll pos) {
    for (int i = pos; i < cur_primes.size(); i++) {
        ll u = cur_primes[i];
        if (v % u == 0) {
            go(v / u, i);
        }
    }
    all.push_back(v);
}

int main() {
    //freopen("input.txt", "r", stdin);
    for (int i = 0; i < 1050000; i++)
        cnt.keys[i] = tot_cnt.keys[i] = total_dp.keys[i] = -1;
    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
            lp[i * pr[j]] = pr[j];
    }
    cin >> n;
    for (int i = 1; i <= n; i++) {
        scanf("%I64d", &a[i]);
        sum += a[i];
    }
    for (int i = 2; i <= n; i++) {
        int x, y;
        scanf("%d", &x);
        g[i].push_back(x);
        g[x].push_back(i);
    }
    //cout << "hyu" << endl;
    dfs(1);
    ll x = sum;
    for (int i = 0; i < pr.size(); i++) {
        if (x % pr[i] == 0) {
            cur_primes.push_back(pr[i]);
            while (x % pr[i] == 0)
                x /= pr[i];
        }
    }
    if (x != 1)
        cur_primes.push_back(x);
    go(sum, 0);
    //for (int i = 0; i < all.size(); i++)
    //    cout << all[i] << endl;
    sort(all.begin(), all.end());
    reverse(all. begin(), all.end());
    for (int j = 0; j < all.size(); j++) {
        sz = 0;
        ll v = all[j];
        for (int i = 0; i < cur_primes.size(); i++)
        if (sum % (v * cur_primes[i]) == 0) {
            b[sz++] = cur_primes[i];
        }
        ll ans = 0;
        for (int i = 1; i < (1<<sz); i++) {
            int bits = 0;
            ll mul = 1;
            for (int k = 0; k < sz; k++)
            if ((i>>k)&1) {
                bits++;
                mul *= b[k];
            }
            if (bits % 2)
                ans += tot_cnt.get(v*mul);
            else
                ans -= tot_cnt.get(v*mul);
        }
        tot_cnt.update(v, cnt.get(v) + ans);
        //cout << v << " " << tot_cnt[v] << " " << cnt[v] << endl;
    }
    for (int i = 0; i < all.size(); i++) {
        ll v = all[i];
        if (sum / v == tot_cnt.get(v)) {
            total_dp.update(v, 1);
            good.push_back(v);
        }
    }
    sort(good.begin(), good.end());
    for (int i = 0; i < good.size(); i++) {
        for (int j = i+1; j < good.size(); j++)
            if (good[j] % good[i] == 0) {
                total_dp.update(good[j], total_dp.get(good[i]));
            }
    }
    cout << total_dp.get(sum) << endl;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define pii pair<ll,ll>
#define x1 dlfjl
#define x2 dkfj
#define x3 dflgkg
#define y1 dskgj
#define y2 dskfj
#define y3 dfhgdh
#define SQ 320
#define PI 3.14159265359
#define MOD 1000000007
#define ROW 5
typedef long long ll;
using namespace std;
ll n,i,j,k,m,q,timer,mx, v,tot,x,y,INF, sz,ans, sum;
ll a[1005000], dp[1005000], b[105];
vector<int> g[1005000];
const int N = 32000000;
int lp[N+15];
vector<int> pr;
vector<ll> cur_primes, all, good;

struct Hashset {
    long long keys[300000];
    int values[300000];
    void update(ll key, int value) {
        int hsh = (key&262143);
        while (keys[hsh] != -1 && keys[hsh] != key) {
            hsh++;
        }
        //cout << hsh << " " << value << endl;
        if (keys[hsh] == -1)
            keys[hsh] = key, values[hsh] = value;
        else {
            values[hsh] += value;
            if (values[hsh] >= MOD)
                values[hsh] -= MOD;
        }
    }
    int get(ll key) {
        int hsh = (key&262143);
        while (keys[hsh] != -1 && keys[hsh] != key) {
            hsh++;
        }
        if (keys[hsh] == -1)
            return 0;
        return values[hsh];
    }
} cnt, tot_cnt, total_dp;

ll gcd(ll a, ll b) {
    return b==0?a:gcd(b, a%b);
}

void dfs(int v, int p = -1) {
    dp[v] = a[v];
    for (auto to: g[v]) {
        if (to != p) {
            dfs(to, v);
            dp[v] += dp[to];
        }
    }
}

void go(ll v, ll pos) {
    for (int i = pos; i < cur_primes.size(); i++) {
        ll u = cur_primes[i];
        if (v % u == 0) {
            go(v / u, i);
        }
    }
    all.push_back(v);
}

int main() {
    //freopen("input.txt", "r", stdin);
    for (int i = 0; i < 300000; i++)
        cnt.keys[i] = tot_cnt.keys[i] = total_dp.keys[i] = -1;
    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
            lp[i * pr[j]] = pr[j];
    }
    cin >> n;
    for (int i = 1; i <= n; i++) {
        scanf("%I64d", &a[i]);
        sum += a[i];
    }
    for (int i = 2; i <= n; i++) {
        int x, y;
        scanf("%d", &x);
        g[i].push_back(x);
        g[x].push_back(i);
    }
    //cout << "hyu" << endl;
    dfs(1);
    for (int v = 1; v <= n; v++) {

        cnt.update(gcd(dp[v], sum), 1);
    }
    ll x = sum;
    for (int i = 0; i < pr.size(); i++) {
        if (x % pr[i] == 0) {
            cur_primes.push_back(pr[i]);
            while (x % pr[i] == 0)
                x /= pr[i];
        }
    }
    if (x != 1)
        cur_primes.push_back(x);
    go(sum, 0);
    //for (int i = 0; i < all.size(); i++)
    //    cout << all[i] << endl;
    sort(all.begin(), all.end());
    reverse(all. begin(), all.end());
    for (int j = 0; j < all.size(); j++) {
        sz = 0;
        ll v = all[j];
        for (int i = 0; i < cur_primes.size(); i++)
        if (sum % (v * cur_primes[i]) == 0) {
            b[sz++] = cur_primes[i];
        }
        ll ans = 0;
        for (int i = 1; i < (1<<sz); i++) {
            int bits = 0;
            ll mul = 1;
            for (int k = 0; k < sz; k++)
            if ((i>>k)&1) {
                bits++;
                mul *= b[k];
            }
            if (bits % 2)
                ans += tot_cnt.get(v*mul);
            else
                ans -= tot_cnt.get(v*mul);
        }
        tot_cnt.update(v, cnt.get(v) + ans);
        //cout << v << " " << tot_cnt[v] << " " << cnt[v] << endl;
    }
    for (int i = 0; i < all.size(); i++) {
        ll v = all[i];
        if (sum / v == tot_cnt.get(v)) {
            total_dp.update(v, 1);
            good.push_back(v);
        }
    }
    sort(good.begin(), good.end());
    for (int i = 0; i < good.size(); i++) {
        for (int j = i+1; j < good.size(); j++)
            if (good[j] % good[i] == 0) {
                total_dp.update(good[j], total_dp.get(good[i]));
            }
    }
    cout << total_dp.get(sum) << endl;
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define pii pair<ll,ll>
#define x1 dlfjl
#define x2 dkfj
#define x3 dflgkg
#define y1 dskgj
#define y2 dskfj
#define y3 dfhgdh
#define SQ 320
#define PI 3.14159265359
#define MOD 1000000007
#define ROW 5
typedef long long ll;
using namespace std;
ll n,i,j,k,m,q,timer,mx, v,tot,x,y,INF, sz,ans, sum;
ll a[1005000], dp[1005000], b[1050000];
vector<int> g[1005000];
vector<ll> divs;
const int N = 32000000;
int lp[N+15];
vector<int> pr;
vector<ll> cur_primes, all, good;

struct Hashset {
    long long keys[300000];
    int values[300000];
    void update(ll key, int value) {
        int hsh = (key&262143);
        while (keys[hsh] != -1 && keys[hsh] != key) {
            hsh++;
        }
        //cout << hsh << " " << value << endl;
        if (keys[hsh] == -1)
            keys[hsh] = key, values[hsh] = value;
        else {
            values[hsh] += value;
            if (values[hsh] >= MOD)
                values[hsh] -= MOD;
        }
    }
    int get(ll key) {
        int hsh = (key&262143);
        while (keys[hsh] != -1 && keys[hsh] != key) {
            hsh++;
        }
        if (keys[hsh] == -1)
            return 0;
        return values[hsh];
    }
} cnt, tot_cnt, total_dp;

ll gcd(ll a, ll b) {
    return b==0?a:gcd(b, a%b);
}

void dfs(int v, int p = -1) {
    dp[v] = a[v];
    for (int i = 0; i < g[v].size(); i++) {
        int to = g[v][i];
        if (to != p) {
            dfs(to, v);
            dp[v] += dp[to];
        }
    }
    /*if (sum % dp[v] == 0 && total_dp.find(dp[v]) == total_dp.end()) {
        total_dp[dp[v]] = 0;
        all.push_back(dp[v]);
    }*/
    cnt.update(gcd(dp[v], sum), 1);
}

void go(ll v, ll pos) {
    for (int i = pos; i < cur_primes.size(); i++) {
        ll u = cur_primes[i];
        if (v % u == 0) {
            go(v / u, i);
        }
    }
    all.push_back(v);
}

int main() {
    //freopen("input.txt", "r", stdin);
    for (int i = 0; i < 300000; i++)
        cnt.keys[i] = tot_cnt.keys[i] = total_dp.keys[i] = -1;
    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
            lp[i * pr[j]] = pr[j];
    }
    cin >> n;
    for (int i = 1; i <= n; i++) {
        scanf("%I64d", &a[i]);
        sum += a[i];
    }
    for (int i = 2; i <= n; i++) {
        int x, y;
        scanf("%d", &x);
        g[i].push_back(x);
        g[x].push_back(i);
    }
    //cout << "hyu" << endl;
    dfs(1);
    ll x = sum;
    for (int i = 0; i < pr.size(); i++) {
        if (x % pr[i] == 0) {
            cur_primes.push_back(pr[i]);
            while (x % pr[i] == 0)
                x /= pr[i];
        }
    }
    if (x != 1)
        cur_primes.push_back(x);
    go(sum, 0);
    //for (int i = 0; i < all.size(); i++)
    //    cout << all[i] << endl;
    sort(all.begin(), all.end());
    reverse(all. begin(), all.end());
    for (int j = 0; j < all.size(); j++) {
        sz = 0;
        ll v = all[j];
        for (int i = 0; i < cur_primes.size(); i++)
        if (sum % (v * cur_primes[i]) == 0) {
            b[sz++] = cur_primes[i];
        }
        ll ans = 0;
        for (int i = 1; i < (1<<sz); i++) {
            int bits = 0;
            ll mul = 1;
            for (int k = 0; k < sz; k++)
            if ((i>>k)&1) {
                bits++;
                mul *= b[k];
            }
            if (bits % 2)
                ans += tot_cnt.get(v*mul);
            else
                ans -= tot_cnt.get(v*mul);
        }
        tot_cnt.update(v, cnt.get(v) + ans);
        //cout << v << " " << tot_cnt[v] << " " << cnt[v] << endl;
    }
    for (int i = 0; i < all.size(); i++) {
        ll v = all[i];
        if (sum / v == tot_cnt.get(v)) {
            total_dp.update(v, 1);
            good.push_back(v);
        }
    }
    sort(good.begin(), good.end());
    for (int i = 0; i < good.size(); i++) {
        for (int j = i+1; j < good.size(); j++)
            if (good[j] % good[i] == 0) {
                total_dp.update(good[j], total_dp.get(good[i]));
            }
    }
    cout << total_dp.get(sum) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
long long a[1000010];
int fa[1000010],n,cnt[1000010],f[1000010],ans=0;
long long gcd(long long x,long long y){
	return y?gcd(y,x%y):x;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	for(int i=2;i<=n;i++) scanf("%d",&fa[i]);
	for(int i=n;i>=2;i--) a[fa[i]]+=a[i];
	for(int i=1;i<=n;i++) if(a[1]/(gcd(a[1],a[i]))<=n) cnt[a[1]/gcd(a[1],a[i])]++;
	for(int i=1;i<=n;i++) for(int j=i;j<=n;j+=i) f[j]+=cnt[i];
	for(int i=1;i<=n;i++) f[i]=(f[i]==i);
	for(int i=n;i>1;i--) if(f[i]){
		for(int j=i+i;j<=n;j+=i) f[i]=(f[i]+f[j])%mod;
		ans=(ans+f[i])%mod;
	}
	printf("%d\n",(ans+1)%mod);
}


/*FBI WARNING
Federal law provides severe civil and criminal penalties for
the unauthorzed reproduction, distribution, or exhibition of
copyrighted motion pictures (Ti2tle 17 united states code
sections 501 and 508). The federal bureau of investigation
investigates allegations of criminal copyright infringement
(Title 17, united states code sections 506).*/

#include<bits/stdc++.h>
using namespace std;
#define maxn 1000010
#define ll long long
#define mod 1000000007

template < typename tp > void read ( tp & dig ) {
    char ch=getchar();ll flag=0;dig=0;
    while(!isdigit(ch)){ if(ch=='-')flag=1;ch=getchar(); }
    while(isdigit(ch))dig=dig*10+ch-'0',ch=getchar();
    if(flag)dig=-dig;
}

int n,a[maxn],fa[maxn];
int f[maxn],num[maxn];

inline int F(int x){
    return x>=mod?x-mod:x;
}

inline int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
#endif

    read(n);int x;
    for(int i=1;i<=n;++i)read(a[i]);
    for(int i=2;i<=n;++i)read(fa[i]);
    for(int i=n;i>1;--i)a[fa[i]]+=a[i];
    for(int i=n;i;--i){
        a[i]=a[1]/gcd(a[i],a[1]);
        if(a[i]<=n)++num[a[i]];
    }for(int i=n;i;--i)
        for(int j=i+i;j<=n;j+=i)
            num[j]=F(num[j]+num[i]);
    f[1]=1;int ans=0;
    for(int i=1;i<=n;++i){
        if(num[i]!=i)continue;
        ans=F(ans+f[i]);
        for(int j=i+i;j<=n;j+=i)
            f[j]=F(f[j]+f[i]);
    }printf("%d\n",ans);
    return 0;

}


/*FBI WARNING
Federal law provides severe civil and criminal penalties for
the unauthorzed reproduction, distribution, or exhibition of
copyrighted motion pictures (Ti2tle 17 united states code
sections 501 and 508). The federal bureau of investigation
investigates allegations of criminal copyright infringement
(Title 17, united states code sections 506).*/

#include<bits/stdc++.h>
using namespace std;
#define maxn 1000010
#define ll long long
#define mod 1000000007

template < typename tp > void read ( tp & dig ) {
    char ch=getchar();ll flag=0;dig=0;
    while(!isdigit(ch)){ if(ch=='-')flag=1;ch=getchar(); }
    while(isdigit(ch))dig=dig*10+ch-'0',ch=getchar();
    if(flag)dig=-dig;
}

ll n,a[maxn],fa[maxn];
ll f[maxn],num[maxn];

inline ll F(ll x){
    return x>=mod?x-mod:x;
}

inline ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
#endif

    read(n);ll x;
    for(ll i=1;i<=n;++i)read(a[i]);
    for(ll i=2;i<=n;++i)read(fa[i]);
    for(ll i=n;i>1;--i)a[fa[i]]+=a[i];
    for(ll i=n;i;--i){
        a[i]=a[1]/gcd(a[i],a[1]);
        if(a[i]<=n)++num[a[i]];
    }for(ll i=n;i;--i)
        for(ll j=i+i;j<=n;j+=i)
            num[j]=F(num[j]+num[i]);
    f[1]=1;ll ans=0;
    for(ll i=1;i<=n;++i){
        if(num[i]!=i)continue;
        ans=F(ans+f[i]);
        for(ll j=i+i;j<=n;j+=i)
            f[j]=F(f[j]+f[i]);
    }printf("%lld\n",ans);
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mo 1000000007ll
#define N 1000010
ll n,a[N],f[N],g[N],p[N],r;
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(int i=2;i<=n;i++)
		scanf("%lld",&p[i]);
	for(int i=n;i;i--)
		a[p[i]]+=a[i];
	for(int i=n;i;i--)
		a[i]=a[1]/__gcd(a[1],a[i]);
	for(int i=1;i<=n;i++)
		if(a[i]<=n)f[a[i]]++;
	for(int i=n;i;i--)
		for(int j=i*2;j<=n;j+=i)
			f[j]=(f[j]+f[i])%mo;
	g[1]=1;
	for(int i=1;i<=n;i++)if(f[i]==i)
		for(int j=i*2;j<=n;j+=i)
			g[j]=(g[j]+g[i])%mo;
	for(int i=1;i<=n;i++)
		if(f[i]==i)r=(r+g[i])%mo;
	cout<<r;
}


#include<algorithm>
#include<cstdio>
#define mxn 1000010
#define LL long long
using namespace std;
int n,sl,fh,a[mxn],b[mxn],num[mxn];
LL sum,ans,s[mxn],f[mxn];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
LL gcd(LL a,LL b)
{
	LL r=a%b;
	for(;r;a=b,b=r,r=a%b);
	return b;
}
void dfs(int u)
{
	int v;s[u]=a[u];
	for(int i=h[u];i;i=e[i].nxt) dfs(v=e[i].to),s[u]+=s[v];
	LL g=sum/gcd(s[u],sum);
	if(g<=n) b[g]++;
}
int main()
{
	n=rd();int x;
	for(int i=1;i<=n;++i) a[i]=rd(),sum+=a[i];
	for(int i=2;i<=n;++i) x=rd(),add(x,i);
	dfs(1);
	for(int i=1;i<=n;++i)
		if(b[i])
			for(int j=i;j<=n;j+=i)
				num[j]+=b[i];
	for(int i=1;i<=n;++i) f[i]=b[i]=i==num[i];
	for(int i=2;i<=n;++i)
		for(int j=i*2;j<=n;j+=i)
			if(b[j])
				f[j]+=f[i];
	for(int i=1;i<=n;++i) ans+=f[i];
	printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstdio>
#define mxn 1000010
#define LL long long
using namespace std;
int n,sl,a[mxn],b[mxn],num[mxn];
LL sum,ans,s[mxn],f[mxn];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn];
inline char gc()
{
	static char buf[1<<16],*S,*T;
	if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(S==T)return EOF;}
	return *S++;
}
inline int rd()
{
	sl=0;
	char ch=gc();
	while(ch<'0'||'9'<ch) ch=gc();
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=gc();
	return sl;
}
inline void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
inline LL gcd(LL a,LL b)
{
	LL r=a%b;
	for(;r;a=b,b=r,r=a%b);
	return b;
}
void dfs(int u)
{
	int v;s[u]=a[u];
	for(int i=h[u];i;i=e[i].nxt) dfs(v=e[i].to),s[u]+=s[v];
	LL g=sum/gcd(s[u],sum);
	if(g<=n) b[g]++;
}
int main()
{
	n=rd();int x;
	for(int i=1;i<=n;++i) a[i]=rd(),sum+=a[i];
	for(int i=2;i<=n;++i) x=rd(),add(x,i);
	dfs(1);
	for(int i=1;i<=n;++i)
		if(b[i])
			for(int j=i;j<=n;j+=i)
				num[j]+=b[i];
	for(int i=1;i<=n;++i) f[i]=b[i]=i==num[i];
	for(int i=2;i<=n;++i)
		for(int j=i*2;j<=n;j+=i)
			if(b[j])
				f[j]+=f[i];
	for(int i=1;i<=n;++i) ans+=f[i];
	printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstdio>
#define mxn 1000010
#define LL long long
using namespace std;
int n,sl,fh,a[mxn],b[mxn],num[mxn];
LL sum,ans,s[mxn],f[mxn];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn];
inline char gc()
{
	static char buf[1<<16],*S,*T;
	if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(S==T)return EOF;}
	return *S++;
}
inline int rd()
{
	sl=0;fh=1;
	char ch=gc();
	while(ch<'0'||'9'<ch) ch=gc();
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=gc();
	return sl*fh;
}
inline void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
inline LL gcd(LL a,LL b)
{
	LL r=a%b;
	for(;r;a=b,b=r,r=a%b);
	return b;
}
void dfs(int u)
{
	int v;s[u]=a[u];
	for(int i=h[u];i;i=e[i].nxt) dfs(v=e[i].to),s[u]+=s[v];
	LL g=sum/gcd(s[u],sum);
	if(g<=n) b[g]++;
}
int main()
{
	n=rd();int x;
	for(int i=1;i<=n;++i) a[i]=rd(),sum+=a[i];
	for(int i=2;i<=n;++i) x=rd(),add(x,i);
	dfs(1);
	for(int i=1;i<=n;++i)
		if(b[i])
			for(int j=i;j<=n;j+=i)
				num[j]+=b[i];
	for(int i=1;i<=n;++i) f[i]=b[i]=i==num[i];
	for(int i=2;i<=n;++i)
		for(int j=i*2;j<=n;j+=i)
			if(b[j])
				f[j]+=f[i];
	for(int i=1;i<=n;++i) ans+=f[i];
	printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<cstdio>
#define mxn 1000010
#define LL long long
using namespace std;
int n,sl,fh,a[mxn],s[mxn],b[mxn],num[mxn];
LL sum,ans,f[mxn];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn];
int rd()
{
	sl=0;fh=1;
	char ch=getchar();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
	return sl*fh;
}
void add(int u,int v) {e[++t]=(Tre){v,h[u]};h[u]=t;}
LL gcd(LL a,LL b)
{
	LL r=a%b;
	for(;r;a=b,b=r,r=a%b);
	return b;
}
void dfs(int u)
{
	int v;s[u]=a[u];
	for(int i=h[u];i;i=e[i].nxt) dfs(v=e[i].to),s[u]+=s[v];
	LL g=sum/gcd(s[u],sum);
	if(g<=n) b[g]++;
}
int main()
{
	n=rd();int x;
	for(int i=1;i<=n;++i) a[i]=rd(),sum+=a[i];
	for(int i=2;i<=n;++i) x=rd(),add(x,i);
	dfs(1);
	for(int i=1;i<=n;++i)
		if(b[i])
			for(int j=i;j<=n;j+=i)
				num[j]+=b[i];
	for(int i=1;i<=n;++i) f[i]=b[i]=i==num[i];
	for(int i=2;i<=n;++i)
		for(int j=i*2;j<=n;j+=i)
			if(b[j])
				f[j]+=f[i];
	for(int i=1;i<=n;++i) ans+=f[i];
	printf("%lld\n",ans);
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn = 2000000 + 10;
const ll MOD = 1000000007;

inline ll inc(ll a, ll b) { return (a + b >= MOD) ? (a + b - MOD) : (a + b); }

ll gcd(ll a, ll b) { if(a % b == 0) return b; else return gcd(b,a % b); }

ll n, val[maxn], p[maxn], s[maxn], f[maxn], ans[maxn];
int main() {
//	freopen("data.in","r",stdin);
	scanf("%I64d", &n);
	for(int i = 1;i <= n;i ++) scanf("%I64d", &val[i]);
	for(int i = 2;i <= n;i ++) scanf("%I64d", &p[i]);
	for(int i = n;i >= 1;i --) val[p[i]] += val[i];
	for(int i = n;i >= 1;i --) s[i] = val[1] / gcd(val[1],val[i]);
	for(int i = 1;i <= n;i ++) if(s[i] <= n) f[s[i]] ++;
	for(int i = n;i >= 1;i --) {
	  for(int j = i + i;j <= n;j += i) f[j] = inc(f[j],f[i]);
	}
	ans[1] = 1;
	for(int i = 1;i <= n;i ++) {
	  if(f[i] >= i) {
	    for(int j = i + i;j <= n;j += i) ans[j] = inc(ans[i],ans[j]);
		}
	}
	ll Ans = 0;
	for(int i = 1;i <= n;i ++) if(f[i] >= i) Ans = inc(Ans,ans[i]);
	printf("%I64d\n", Ans);
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 1000000 + 10;
const int MOD = 1000000007;

inline int inc(int a, int b) { return (a + b >= MOD) ? (a + b - MOD) : (a + b); }

int gcd(int a, int b) { if(a % b == 0) return b; else return gcd(b,a % b); }

int n, val[maxn], p[maxn], s[maxn], f[maxn], ans[maxn];
int main() {
//	freopen("data.in","r",stdin);
	scanf("%d", &n);
	for(int i = 1;i <= n;i ++) scanf("%d", &val[i]);
	for(int i = 2;i <= n;i ++) scanf("%d", &p[i]);
	for(int i = n;i >= 1;i --) val[p[i]] += val[i];
	for(int i = n;i >= 1;i --) s[i] = val[1] / gcd(val[1],val[i]);
	for(int i = 1;i <= n;i ++) if(s[i] <= n) f[s[i]] ++;
	for(int i = n;i >= 1;i --) {
	  for(int j = i + i;j <= n;j += i) f[j] = inc(f[j],f[i]);
	}
	ans[1] = 1;
	for(int i = 1;i <= n;i ++) {
	  if(f[i] >= i) {
	    for(int j = i + i;j <= n;j += i) ans[j] = inc(ans[i],ans[j]);
		}
	}
	int Ans = 0;
	for(int i = 1;i <= n;i ++) if(f[i] >= i) Ans = inc(Ans,ans[i]);
	printf("%d\n", Ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define per(i, a, b) for(int i=(b)-1; i>=(a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll<<(x))
#define endl "\n"
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int P = 1e9 + 7;
int add(int a, int b) {if((a += b) >= P) a -= P; return a;}
int sub(int a, int b) {if((a -= b) < 0) a += P; return a;}
int mul(int a, int b) {return 1ll * a * b % P;}
int kpow(int a, int b) {int r=1;for(;b;b>>=1,a=mul(a,a)) {if(b&1)r=mul(r,a);}return r;}
//----

const int N = 1e6 + 7;
vector<ll> p, d;
ll s[N], sum, ss;
int a[N], n, mi, x, f[100000];
vi g[N];
bool ok[N];
void dfs(int u) {
	s[u] = a[u];
	rep(i, 0, sz(g[u])) {
		int v = g[u][i];
		dfs(v);
		s[u] += s[v];
	}
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	//cout << setiosflags(ios::fixed);
	//cout << setprecision(2);
	cin >> n;
	mi = pw(30);
	rep(i, 1, n+1) cin >> a[i], sum += a[i], mi = min(mi, a[i]);
	ss = sum;
	rep(i, 2, n+1) cin >> x, g[x].pb(i);
	for (ll x = 2; x * x <= sum; x++) {
		if (sum % x == 0) p.pb(x);
		while (sum % x == 0) sum /= x;
	}
	p.pb(1);
	if (sum > 1) p.pb(sum);

	for (ll x = 1; x * x <= ss; x++) if (ss % x == 0) {
		d.pb(x);
		if (x * x != ss) d.pb(ss / x);
	}

	sort(all(d));

	sort(all(p));
	p.erase(upper_bound(all(p), mi), p.end());
	dfs(1);
	//rep(i, 1, n+1) de(s[i]);
	rep(i, 0, sz(d)) {
		int cnt = 0;
		rep(j, 1, n+1) if (s[j] % d[i] == 0) cnt++;
		if (cnt == ss / d[i]) f[i] = 1, ok[i] = 1;
		if (ok[i]) rep(j, 0, i) if (ok[j] && d[i] % d[j] == 0) f[i] = add(f[i], f[j]);
	}
	cout << f[sz(d)-1];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define per(i, a, b) for(int i=(b)-1; i>=(a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll<<(x))
#define endl "\n"
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int P = 1e9 + 7;
int add(int a, int b) {if((a += b) >= P) a -= P; return a;}
int sub(int a, int b) {if((a -= b) < 0) a += P; return a;}
int mul(int a, int b) {return 1ll * a * b % P;}
int kpow(int a, int b) {int r=1;for(;b;b>>=1,a=mul(a,a)) {if(b&1)r=mul(r,a);}return r;}
//----

const int N = 1e6 + 7;
vector<ll> p, d;
ll s[N], sum, ss;
int a[N], n, mi, x, f[100000];
vi g[N];
bool ok[N];
void dfs(int u) {
	s[u] = a[u];
	rep(i, 0, sz(g[u])) {
		int v = g[u][i];
		dfs(v);
		s[u] += s[v];
	}
}

unordered_set<ll> S;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	//cout << setiosflags(ios::fixed);
	//cout << setprecision(2);
	cin >> n;
	mi = pw(30);
	rep(i, 1, n+1) cin >> a[i], sum += a[i], mi = min(mi, a[i]);
	ss = sum;
	rep(i, 2, n+1) cin >> x, g[x].pb(i);
/*	for (ll x = 2; x * x <= sum; x++) {
		if (sum % x == 0) p.pb(x);
		while (sum % x == 0) sum /= x;
	}
	p.pb(1);
	if (sum > 1) p.pb(sum);
*/
	for (ll x = 1; x * x <= ss; x++) if (ss % x == 0) {
		d.pb(x);
		if (x * x != ss) d.pb(ss / x);
	}

	sort(all(d));

//	sort(all(p));
//	p.erase(upper_bound(all(p), mi), p.end());
	dfs(1);
	//rep(i, 1, n+1) de(s[i]);
	rep(i, 1, n+1) S.insert(s[i]);
	rep(i, 0, sz(d)) {
		int cnt = 0;
		if (ss / d[i] <= n && S.count(d[i])) {
			rep(j, 1, n+1) if (s[j] % d[i] == 0) cnt++;
			if (cnt == ss / d[i]) f[i] = 1, ok[i] = 1;
		}
		if (ok[i]) rep(j, 0, i) if (ok[j] && d[i] % d[j] == 0) f[i] = add(f[i], f[j]);
	}
	cout << f[sz(d)-1];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define per(i, a, b) for(int i=(b)-1; i>=(a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll<<(x))
#define endl "\n"
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int P = 1e9 + 7;
int add(int a, int b) {if((a += b) >= P) a -= P; return a;}
int sub(int a, int b) {if((a -= b) < 0) a += P; return a;}
int mul(int a, int b) {return 1ll * a * b % P;}
int kpow(int a, int b) {int r=1;for(;b;b>>=1,a=mul(a,a)) {if(b&1)r=mul(r,a);}return r;}
//----

const int N = 1e6 + 7;
vector<ll> d;
ll s[N], sum, ss, cc[N];
int a[N], n, x, f[100000];
vi g[N];
bool ok[N];
void dfs(int u) {
	s[u] = a[u];
	rep(i, 0, sz(g[u])) {
		int v = g[u][i];
		dfs(v);
		s[u] += s[v];
	}
}

unordered_map<ll, int> S;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	//cout << setiosflags(ios::fixed);
	//cout << setprecision(2);
	cin >> n;
	rep(i, 1, n+1) cin >> a[i], sum += a[i];
	rep(i, 2, n+1) cin >> x, g[x].pb(i);

	for (ll x = 1; x * x <= sum; x++) if (sum % x == 0) {
		d.pb(x);
		if (x * x != sum) d.pb(sum / x);
	}
	sort(all(d));

	dfs(1);
	rep(i, 0, sz(d)) S[d[i]] = i;

	rep(i, 1, n+1) cc[S[__gcd(sum, s[i])]] ++;

	rep(i, 0, sz(d))
		rep(j, i+1, sz(d)) if (d[j] % d[i] == 0) cc[i] += cc[j];
	rep(i, 0, sz(d)) {
		if (cc[i] == sum / d[i]) f[i] = 1, ok[i] = 1;
		if (ok[i]) rep(j, 0, i) if (ok[j] && d[i] % d[j] == 0) f[i] = add(f[i], f[j]);
	}
	cout << f[sz(d)-1];
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define per(i, a, b) for(int i=(b)-1; i>=(a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll<<(x))
#define endl "\n"
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int P = 1e9 + 7;
int add(int a, int b) {if((a += b) >= P) a -= P; return a;}
int sub(int a, int b) {if((a -= b) < 0) a += P; return a;}
int mul(int a, int b) {return 1ll * a * b % P;}
int kpow(int a, int b) {int r=1;for(;b;b>>=1,a=mul(a,a)) {if(b&1)r=mul(r,a);}return r;}
//----

const int N = 1e6 + 7;
vector<ll> p, d;
ll s[N], sum, ss;
int a[N], n, mi, x, f[100000];
vi g[N];
bool ok[N];
void dfs(int u) {
	s[u] = a[u];
	rep(i, 0, sz(g[u])) {
		int v = g[u][i];
		dfs(v);
		s[u] += s[v];
	}
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	//cout << setiosflags(ios::fixed);
	//cout << setprecision(2);
	cin >> n;
	mi = pw(30);
	rep(i, 1, n+1) cin >> a[i], sum += a[i], mi = min(mi, a[i]);
	ss = sum;
	rep(i, 2, n+1) cin >> x, g[x].pb(i);
/*	for (ll x = 2; x * x <= sum; x++) {
		if (sum % x == 0) p.pb(x);
		while (sum % x == 0) sum /= x;
	}
	p.pb(1);
	if (sum > 1) p.pb(sum);
*/
	for (ll x = 1; x * x <= ss; x++) if (ss % x == 0) {
		d.pb(x);
		if (x * x != ss) d.pb(ss / x);
	}

	sort(all(d));

//	sort(all(p));
//	p.erase(upper_bound(all(p), mi), p.end());
	dfs(1);
	//rep(i, 1, n+1) de(s[i]);
	rep(i, 0, sz(d)) {
		int cnt = 0;
		if (ss / d[i] <= n) {
			rep(j, 1, n+1) if (s[j] % d[i] == 0) cnt++;
			if (cnt == ss / d[i]) f[i] = 1, ok[i] = 1;
		}
		if (ok[i]) rep(j, 0, i) if (ok[j] && d[i] % d[j] == 0) f[i] = add(f[i], f[j]);
	}
	cout << f[sz(d)-1];
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mxn=1000010,md=1000000007;
LL gcd(LL x,LL y){
	return !y?x:gcd(y,x%y);
}
int n,m,head[mxn],a[mxn],cnt[mxn],f[mxn];
LL S,sum[mxn];
struct ed{int to,nxt;}edge[mxn];
void addedge(int u,int v){
	edge[++m]=(ed){v,head[u]},head[u]=m;
}
void dfs(int u){
	sum[u]=a[u];
	for (int i=head[u],v;i;i=edge[i].nxt)
		dfs(v=edge[i].to),sum[u]+=sum[v];
	LL x=S/gcd(sum[u],S);
	if (x<=n) ++cnt[x];
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;++i) scanf("%d",&a[i]),S+=a[i];
	for (int i=2,x;i<=n;++i) scanf("%d",&x),addedge(x,i);
	dfs(1);
	memset(sum,0,sizeof(sum));
	for (int i=1;i<=n;++i)
		for (int j=i;j<=n;j+=i) sum[j]+=cnt[i];
	memset(cnt,0,sizeof(cnt));
	for (int i=1;i<=n;++i) cnt[i]=sum[i]>=i;
	f[1]=1;
	int ans=0;
	for (int i=1;i<=n;++i){
		ans=(ans+f[i])%md;
		for (int j=i+i;j<=n;j+=i)
			if (cnt[j]) f[j]=(f[j]+f[i])%md;
	}
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mxn=1000010,md=1000000007;
LL gcd(LL x,LL y){
	return !y?x:gcd(y,x%y);
}
int n,m,head[mxn],a[mxn],cnt[mxn],f[mxn];
LL S,sum[mxn];
struct ed{int to,nxt;}edge[mxn];
void addedge(int u,int v){
	edge[++m]=(ed){v,head[u]},head[u]=m;
}
void dfs(int u){
	sum[u]=a[u];
	for (int i=head[u],v;i;i=edge[i].nxt)
		dfs(v=edge[i].to),sum[u]+=sum[v];
	int x=S/gcd(sum[u],S);
	if (x<=n) ++cnt[x];
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;++i) scanf("%d",&a[i]),S+=a[i];
	for (int i=2,x;i<=n;++i) scanf("%d",&x),addedge(x,i);
	dfs(1);
	memset(sum,0,sizeof(sum));
	for (int i=1;i<=n;++i)
		for (int j=i;j<=n;j+=i) sum[j]+=cnt[i];
	memset(cnt,0,sizeof(cnt));
	for (int i=1;i<=n;++i) cnt[i]=sum[i]>=i;
	f[1]=1;
	int ans=0;
	for (int i=1;i<=n;++i){
		ans=(ans+f[i])%md;
		for (int j=i+i;j<=n;j+=i)
			if (cnt[j]) f[j]=(f[j]+f[i])%md;
	}
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int mxn=1000010,md=1000000009;
LL gcd(LL x,LL y){
	return !y?x:gcd(y,x%y);
}
int n,m,head[mxn],a[mxn],cnt[mxn],f[mxn];
LL S,sum[mxn];
struct ed{int to,nxt;}edge[mxn];
void addedge(int u,int v){
	edge[++m]=(ed){v,head[u]},head[u]=m;
}
void dfs(int u){
	sum[u]=a[u];
	for (int i=head[u],v;i;i=edge[i].nxt)
		dfs(v=edge[i].to),sum[u]+=sum[v];
	LL x=S/gcd(sum[u],S);
	if (x<=n) ++cnt[x];
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;++i) scanf("%d",&a[i]),S+=a[i];
	for (int i=2,x;i<=n;++i) scanf("%d",&x),addedge(x,i);
	dfs(1);
	memset(sum,0,sizeof(sum));
	for (int i=1;i<=n;++i)
		for (int j=i;j<=n;j+=i) sum[j]+=cnt[i];
	memset(cnt,0,sizeof(cnt));
	for (int i=1;i<=n;++i) cnt[i]=sum[i]>=i;
	f[1]=1;
	int ans=0;
	for (int i=1;i<=n;++i){
		ans=(ans+f[i])%md;
		for (int j=i+i;j<=n;j+=i)
			if (cnt[j]) f[j]=(f[j]+f[i])%md;
	}
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = (int)1e6 + 1, maxm = 26881, mod = (int)1e9 + 7;
int n, m, a[maxn], fa[maxn], lnk[maxn], nxt[maxn], c[maxn], f[maxn];
LL s[maxn], que[maxn];
map<LL, int> Hash;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", a + i);
		fa[i] = lnk[i] = nxt[i] = -1;
	}
	for(int i = 2; i <= n; ++i) {
		scanf("%d", fa + i);
		nxt[i] = lnk[fa[i]];
		lnk[fa[i]] = i;
	}
	for(int i = n; i >= 1; --i) {
		s[i] += a[i];
		if(fa[i] > 0)
			s[fa[i]] += s[i];
	}
	for(int i = 1; i <= n; ++i)
		++Hash[__gcd(s[i], s[1])];
	m = 0;
	for(auto &it : Hash) {
		que[m] = it.first;
		c[m++] = it.second;
	}
	for(int i = 0; i < m; ++i) {
		int ctr = c[i];
		for(int j = i + 1; j < m; ++j)
			if(que[j] % que[i] == 0)
				ctr += c[j];
		if(ctr * que[i] != s[1])
			continue;
		f[i] = 1;
		for(int j = 0; j < i; ++j)
			if(que[i] % que[j] == 0)
				(f[i] += f[j]) >= mod && (f[i] -= mod);
	}
	printf("%d\n", f[m - 1]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int Maxn=1000010;
const int inf=2147483647;
const int mod=1000000007;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return x*f;
}
LL s[Maxn];
int n,fa[Maxn],f[Maxn],ans[Maxn];
int main()
{
	memset(f,0,sizeof(f));
	memset(ans,0,sizeof(ans));
	n=read();
	for(int i=1;i<=n;i++)s[i]=read();
	for(int i=2;i<=n;i++)fa[i]=read();
	for(int i=n;i>1;i--)s[fa[i]]+=s[i];
	for(int i=1;i<=n;i++)
	if(s[1]%i==0)
	{
		for(int j=1;j<=n;j++)
		if(s[j]%(s[1]/i)==0)f[i]++;
	}
	ans[1]=1;int Ans=0;
	for(int i=1;i<=n;i++)
	if(f[i]>=i)
	{
		Ans=(Ans+ans[i])%mod;
		for(int j=(i<<1);j<=n;j+=i)
		ans[j]=(ans[j]+ans[i])%mod;
	}
	printf("%d",Ans);
}


#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int Maxn=1000010;
const int inf=2147483647;
const int mod=1000000007;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return x*f;
}
LL s[Maxn];
int n,fa[Maxn],f[Maxn],ans[Maxn];
LL gcd(LL a,LL b)
{
	if(!a)return b;
	return gcd(b%a,a);
}
int main()
{
	memset(f,0,sizeof(f));
	memset(ans,0,sizeof(ans));
	n=read();
	for(int i=1;i<=n;i++)s[i]=read();
	for(int i=2;i<=n;i++)fa[i]=read();
	for(int i=n;i>1;i--)s[fa[i]]+=s[i];
	for(int i=n;i;i--)s[i]=s[1]/gcd(s[1],s[i]);
	for(int i=1;i<=n;i++)if(s[i]<=n)f[s[i]]++;
	for(int i=n;i;i--)
	for(int j=(i<<1);j<=n;j+=i)
	f[j]+=f[i];
	ans[1]=1;int Ans=0;
	for(int i=1;i<=n;i++)
	if(f[i]>=i)
	{
		Ans=(Ans+ans[i])%mod;
		for(int j=(i<<1);j<=n;j+=i)
		ans[j]=(ans[j]+ans[i])%mod;
	}
	printf("%d",Ans);
}


#include <bits/stdc++.h>
using namespace std;
int t,n,x,y,d;
const int inf=1000000010;
int dis(int x,int y){
    return (abs(x-y)+d-1)/d;
}
int main()
{
    scanf("%d",&t);
    while(t--){
        int res=inf;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(abs(x-y)%d==0)printf("%d\n",abs(x-y)/d);
        else{
        if((y-1)%d==0)res=min(dis(x,1)+(y-1)/d,res);
        if((n-y)%d==0)res=min(dis(x,n)+(n-y)/d,res);
        if(res==inf)printf("-1\n");
        else printf("%d\n",res);
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
int t,n,x,y,d;
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if((y-x)%d==0)
            printf("%d\n",(y-x)/d);
        else if((n-y)%d==0)
            printf("%d\n",(n-x+1)/d+(n-y)/d);
        else if((y-1)%d==0)
            printf("%d\n",(x-1+1)/d+(y-1)/d);
        else printf("-1\n");
    }
}


#include <bits/stdc++.h>
using namespace std;
int t,n,x,y,d;
const int inf=1000000010;
int dis(int x,int y){
    return (abs(x-y)+d-1)/d;
}
int main()
{
    scanf("%d",&t);
    while(t--){
        int res=inf;
        scanf("%d%d%d%d",&n,&x,&y,&d);
        if(abs(x-y)%d==0)printf("%d\n",abs(x-y)/d);
        if((y-1)%d==0)res=min(dis(x,1),res);
        if((n-y)%d==0)res=min(dis(x,n),res);
        if(res==inf)printf("-1\n");
        else printf("%d\n",res);
    }
}