#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN];
INT arr[100000],m;
INT sum[NN],key;

bool dfs(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(v==fa) continue;
    if(!dfs(v,u)) return 0;
    sum[u]+=sum[v];
  }
  if(sum[u]>key) return 0;
  if(sum[u]==key) sum[u]=0;
  return 1;
}

int dp[100000];
bool can[100000];
bool flag[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n;
  gn(n);
  INT tot=0;
  for(int i=1; i<=n; i++) {
    gn(a[i]);
    tot+=a[i];
  }
  for(int i=2,d; i<=n; i++){
    gn(d);
    add(i,d);
  }

  INT tmp=tot;
  for(int i=1; (INT)i*i<=tmp; i++) if(tmp%i==0){
    arr[m++]=i;
    arr[m++]=tmp/i;
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=m-1; i>=0; i--)if(!flag[i]){
    key=arr[i];
    if(dfs(1)) {
      if(sum[1]==0) can[i]=1;
    }
    if(!can[i]){
      for(int j=0; j<i; j++) if(arr[i]%arr[j]==0) flag[j]=1;
    }
  }

  for(int i=0; i<m; i++){
    if(can[i]){
      dp[i]=1;
      for(int j=0; j<i; j++) if(can[j] && arr[i]%arr[j]==0){
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
int dp[NN];
int root;
int n;
unordered_map<INT, int> mp;

int calc(INT u) {
	INT val = u;
	int ans = 0;
	while(val <= sz[1]) {
		if(mp.count(val)) ans += mp[val];
		val += u;
	}
	if(u * ans == sz[1]) return 1;
	return 0;
}
VI res[NN/10];
INT ARR[NN];
int main(){
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) mp[sz[i]]++;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	int mm = 1;
	for(int i=1; i<nn; i++) if(calc(arr[i])) {
		ARR[mm++] = arr[i];
	}
	for(int i=1; i<mm; i++) arr[i] = ARR[i];
	nn = mm;
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
int son[NN];
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	son[u] = 1;
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		son[u] += son[v];
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]){
		int v = to[e];
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN/10];
int dp[NN];
int root;
int n;
int rdfs(int u, int fa = 0) {
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		if(son[v] > n/2) return rdfs(v, u);
	}
	return u;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	root = rdfs(1);
	pre_dfs(root);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		nn++;
	}
	if(nn > 200) assert(0);
	nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		if(calc(root, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
int dp[NN];
int root;
int n;
map<INT, int> mp;
int calc(int u) {
	INT val = sz[u];
	int ans = 0;
	while(val <= sz[1]) {
		if(mp.count(val)) ans += mp[val];
		val += sz[u];
	}
	if(sz[u] * ans == sz[1]) return 1;
	return 0;
}
VI res[NN/10];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) mp[sz[i]]++;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		if(calc(i)) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN];
INT arr[100000],m;
INT sum[NN],key;

void search(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(fa==v) continue;
    search(v,u);
    sum[u]+=sum[v];
  }
}

bool dfs(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(v==fa) continue;
    if(!dfs(v,u)) return 0;
    sum[u]+=sum[v];
  }
  if(sum[u]>key) return 0;
  if(sum[u]==key) sum[u]=0;
  return 1;
}

int dp[100000];
bool can[100000];
bool flag[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n;
  gn(n);
  INT tot=0;
  for(int i=1; i<=n; i++) {
    gn(a[i]);
    tot+=a[i];
  }
  for(int i=2,d; i<=n; i++){
    gn(d);
    add(i,d);
  }

  search(1);
  for(int i=1; i<=n; i++) if(sum[1]%sum[i]==0){
    arr[m++]=sum[i];
  }
  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=m-1; i>=0; i--)if(!flag[i]){
    key=arr[i];
    if(dfs(1)) {
      if(sum[1]==0) can[i]=1;
    }
  }

  for(int i=0; i<m; i++){
    if(can[i]){
      dp[i]=1;
      for(int j=0; j<i; j++) if(can[j] && arr[i]%arr[j]==0){
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN],M;
INT arr[100000],m;
INT sum[NN];
unordered_map<INT,int> is_in;

void search(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(fa==v) continue;
    search(v,u);
    sum[u]+=sum[v];
  }
  is_in[sum[u]]++;
}

int dp[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n; gn(n);
  for(int i=1; i<=n; i++)gn(a[i]);
  for(int i=2,d; i<=n; i++){
    gn(d);add(i,d);
  }
  search(1);
  for(int i=1; i<=n; i++) if(sum[1]%sum[i]==0 && sum[1]/sum[i]<=n){
    arr[m++]=sum[i];
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=0; i<m; i++){
    int cnt=0;
    INT tmp=arr[i];
    while(tmp<=sum[1]){
      if(is_in.count(tmp)) cnt+=is_in[tmp];
      tmp+=arr[i];
    }
    if(cnt*arr[i]==sum[1]) arr[M++]=arr[i];
  }
  m=M;

  for(int i=0; i<m; i++){
    dp[i]=1;
    for(int j=0; j<i; j++){
      if(arr[i]%arr[j]==0) {
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
int son[NN];
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	son[u] = 1;
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		son[u] += son[v];
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]){
		int v = to[e];
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN/10];
int dp[NN];
int root;
int n;
int rdfs(int u, int fa = 0) {
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		if(son[v] > n/2) return rdfs(v, u);
	}
	return u;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	root = rdfs(1);
	pre_dfs(root);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		nn++;
	}
	if(nn > 100) assert(0);
	nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		if(calc(root, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];

void add_edge(int u, int v, int c){
	to[E]=v; cap[E]=c; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; cap[E]=0; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];

int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN];
int dp[NN];
VI rev[NN];
int deg[NN];
int ARR[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n; gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		adj[x].pb(i);
		deg[x]++;
		rev[i].pb(x);
	}
	pre_dfs(1);
	int nn = 1;
	if(nn > 100) assert(0);
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		if(calc(1, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
int son[NN];
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	son[u] = 1;
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		son[u] += son[v];
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]){
		int v = to[e];
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN/10];
int dp[NN];
int root;
int n;
int rdfs(int u, int fa = 0) {
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		if(son[v] > n/2) return rdfs(v, u);
	}
	return u;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	root = rdfs(1);
	pre_dfs(root);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		if(calc(root, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl;
#define Get(x,y) scanf("%I64d%I64d",&x,&y);
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define Gett(x,y) scanf("%d%d",&x,&y);
#define f(i,a,b) for(int i=a;i<b;i++)
#define get(x) scanf("%I64d",&x);
#define gett(x) scanf("%d",&x);
#define ops(x) cout<<x<<" !"<<endl;
#define maxm(a,b) a=max(a,b);
#define minm(a,b) a=min(a,b);
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=1e6+2,M=123,mod=1e9+7;

int n,x,ans=1,a[N],dp[M];
ll sum,sub[N];
vector<int> c,g[N];

void dfs(int x){
	sub[x]=a[x];
	f(i,0,g[x].size()){
		dfs(g[x][i]);
		sub[x]+=sub[g[x][i]];
	}
}
void solve(ll x){
	int t=0;
	f(i,2,n+1)
		if(sub[i]%x==0)
			t++;
	if(t==sum/x-1)
		c.pb(sum/x);
}

int main(){
	cin>>n;
	f(i,1,n+1){ gett(a[i]); sum+=a[i]; }
	f(i,2,n+1){
		gett(x);
		g[x].pb(i);
	}
	dfs(1);
	f(i,2,n+1)
		if(sum%i==0)
			solve(sum/i);
	f_(i,c.size()-1,0){
		dp[i]=1;
		f(j,i+1,c.size())
			if(c[j]%c[i]==0)
				dp[i]=(dp[i]+dp[j])%mod;
		ans=(ans+dp[i])%mod;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl;
#define Get(x,y) scanf("%I64d%I64d",&x,&y);
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define Gett(x,y) scanf("%d%d",&x,&y);
#define f(i,a,b) for(int i=a;i<b;i++)
#define get(x) scanf("%I64d",&x);
#define gett(x) scanf("%d",&x);
#define ops(x) cout<<x<<" !"<<endl;
#define maxm(a,b) a=max(a,b);
#define minm(a,b) a=min(a,b);
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=1e6+2,M=130;

ll n,x,gc,ans=1,a[N],cnt[N],dp[M];
ll sum,sub[N];
vector<ll> c,g[N];

void dfs(int x){
	sub[x]=a[x];
	f(i,0,g[x].size()){
		dfs(g[x][i]);
		sub[x]+=sub[g[x][i]];
	}
}
void solve(ll x){
	int t=0;
	f(i,2,n+1)
		if(sub[i]%x==0)
			t++;
	if(t==sum/x-1)
		c.pb(sum/x);
}

int main(){
	cin>>n;
	f(i,1,n+1){ gett(a[i]); sum+=a[i]; gc=__gcd(gc,a[i]); }
	f(i,2,n+1){
		gett(x);
		g[x].pb(i);
	}
	dfs(1);
	f(i,1,n+1)
		cnt[min(1ll*(N-1),sub[1]/__gcd(sub[i],sub[1]))]++;
	f_(i,n,1)
		for(int j=i*2;j<=n;j+=i)
			cnt[j]+=cnt[i];
	f(i,1,n+1)
		if(cnt[i]==i)
			c.pb(i);
	f_(i,c.size()-1,0){
		dp[i]=1;
		f(j,i+1,c.size())
			if(c[j]%c[i]==0)
				dp[i]=dp[i]+dp[j];
	}
	cout<<dp[0];
}


#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int N = (int)1e6 + 123, mod = (int)1e9 + 7, inf = (int)1e9;

vector<int> g[N];
int n, ans, dp[N], cnt[N];
ll s, a[N];
ll dfs(int v){
	for(auto to:g[v]){
		dfs(to);
		a[v] += a[to];
	}
}
int main(){
	scanf("%d",&n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
		s += a[i];
	}
	for(int i = 2; i <= n; i++){
		int x;
		scanf("%d", &x);
		g[x].pb(i);
	}
	dfs(1);

	for(int i = 1; i <= n; i++){
		a[i] = __gcd(s,a[i]);
	    if(s/a[i] < N)
        	cnt[s/a[i]]++;
	}

	for(int i = n; i >= 1; i--){
		if(s % i == 0){
			int cur = 0;
			for(int j = 1; j*j <= i; j ++){
				if(i % j == 0){
					cur += cnt[j];
					if(j*j!=i)
						cur += cnt[i/j];

				}
			}

			if(cur != i)
				continue;
			dp[i] = 1;
			if(i > 1){
				for(int j = i + i; j <= n; j += i){
					dp[i] += dp[j];
					if(dp[i] >= mod)
						dp[i] -= mod;
				}
			}
			ans += dp[i];
			if(ans >= mod)
				ans -= mod;
		}
	}
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1000000007;
int a[1010101],p[1010101];
int cnt[1010101];
int dp[1010101];
long long sum[1010101];
long long gcd(long long x,long long y)
{
	if(y==0)
		return x;
	return gcd(y,x%y);
}
int main()
{
	int n,ans=0;
	long long k;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	memset(sum,0,sizeof(sum));
	for(int i=2;i<=n;i++)
		scanf("%d",&p[i]);
	for(int i=n;i>=1;i--)
	{
		sum[i]+=a[i];
		if(p[i])
			sum[p[i]]+=sum[i];
	}
	for(int i=1;i<=n;i++)
	{
		k=sum[1]/gcd(sum[1],sum[i]);
		if(k<=n)
			cnt[k]++;
	}
	for(int i=n;i>=1;i--)
		for(int j=i+i;j<=n;j+=i)
			cnt[j]+=cnt[i];
	dp[1]=1;
	for(int i=1;i<=n;i++)
		if(cnt[i]==i)
		{
			(ans+=dp[i])%=mod;
			for(int j=i+i;j<=n;j+=i)
				(dp[j]+=dp[i])%=mod;
		}
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1000000007;
int a[1010101],p[1010101];
int cnt[1010101];
int dp[1010101];
long long sum[1010101];
int gcd(int x,int y)
{
	if(y==0)
		return x;
	return gcd(y,x%y);
}
int main()
{
	int n,ans=0;
	long long k;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	memset(sum,0,sizeof(sum));
	for(int i=2;i<=n;i++)
		scanf("%d",&p[i]);
	for(int i=n;i>=1;i--)
	{
		sum[i]+=a[i];
		if(p[i])
			sum[p[i]]+=sum[i];
	}
	for(int i=1;i<=n;i++)
	{
		k=sum[1]/gcd(sum[1],sum[i]);
		if(k<=n)
			cnt[k]++;
	}
	for(int i=n;i>=1;i--)
		for(int j=i+i;j<=n;j+=i)
			cnt[j]+=cnt[i];
	dp[1]=1;
	for(int i=1;i<=n;i++)
		if(cnt[i]==i)
		{
			(ans+=dp[i])%=mod;
			for(int j=i+i;j<=n;j+=i)
				(dp[j]+=dp[i])%=mod;
		}
	printf("%d",ans);
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define rep(i,s,t) for(register int i=s;i<=t;++i)
using namespace std;
const int N=2e6+11,mod=1e9+7;
typedef long long ll;
int p[N],n,cnt[N];
ll sum[N];
int Ans,ans[N];
inline ll gcd(ll a,ll b){
	return b?gcd(b,a%b):a;
}
inline void inc(int &x,int y){
	x+=y;
	if(x>=mod)
		x-=mod;
}
int main(){
	scanf("%d",&n);
	rep(i,1,n)
		scanf("%lld",sum+i);
	rep(i,2,n)
		scanf("%d",p+i);
	for(int i=n;i>1;--i)
		sum[p[i]]+=sum[i];
	rep(i,1,n){
		ll x=sum[1]/gcd(sum[1],sum[i]);
		if(x<=n)++cnt[x];
	}
	for(int i=n;i;--i){
		for(int j=i+i;j<=n;j+=i)
			cnt[j]+=cnt[i];
	}
	ans[1]=1;
	for(int i=1;i<=n;++i)
		if(cnt[i]>=i){
			for(int j=i+i;j<=n;j+=i)
				inc(ans[j],ans[i]);
			inc(Ans,ans[i]);
		}
	printf("%d\n",Ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAXN 1000005
#define MAXM 2000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define ri register int
#define int ll
#define debug(i) (cout << "BREAKPOINT No." << i << endl)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

template <typename T> inline void chkmin(T &a, T b) {a > b ? a : a = b;}
template <typename T> inline void chkmax(T &a, T b) {a < b ? a : a = b;}

using namespace std;

namespace fast_IO
{
	#define gets getchar
/*	inline char gets()
	{
		static char buf[100000], *p1, *p2;
		return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
	}*/

	inline int read_int()
	{
		register int ret = 0, f = 1; register char c = gets();
		while(c < '0' || c > '9') {if(c == '-') f = -1; c = gets();}
		while(c >= '0' && c <= '9') {ret = (ret << 1) + (ret << 3) + int(c - 48); c = gets();}
		return ret * f;
	}
	#undef gets
}using namespace fast_IO;

int N;
int res = 0;
int tot = 0;
int w[MAXN];
int f[MAXN];
int p[MAXN];
int ans[MAXN];

inline void init()
{
	N = read_int();
	for(ri i = 1; i <= N; i++)
		w[i] = read_int();
	for(ri i = 2; i <= N; i++)
		p[i] = read_int();
	for(ri i = N; i; i--)
		w[p[i]] += w[i];
	for(ri i = N; i; i--)
		w[i] = w[1] / __gcd(w[1], w[i]);
	for(ri i = 1; i <= N; i++)
		if(w[i] <= N)
			f[w[i]]++;
	for(ri i = N; i; i--)
		for(ri j = i * 2; j <= N; j += i)
			f[j] += f[i];
	ans[1] = 1;
	for(ri i = 1; i <= N; i++)
		if(f[i] >= i)
			for(ri j = i * 2; j <= N; j += i)
				ans[j] += ans[i], ans[j] %= MOD;
	for(ri i = 1; i <= N; i++)
		if(f[i] >= i)
			res += ans[i], res %= MOD;
	printf("%lld\n", res);
}

signed main()
{
	init();
	return 0;
}


#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
	using namespace std;
	#define pii pair<int,int>
	#define fir first
	#define sec second
	#define MP make_pair
	#define rep(i,x,y) for (int i=(x);i<=(y);i++)
	#define drep(i,x,y) for (int i=(x);i>=(y);i--)
	#define go(x) for (int i=head[x];i;i=edge[i].nxt)
	#define templ template<typename T>
	#define sz 1010101
	typedef long long ll;
	typedef double db;
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
	templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
	templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
	templ inline void read(T& t)
	{
		t=0;char f=0,ch=getchar();double d=0.1;
		while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
		while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
		if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
		t=(f?-t:t);
	}
	template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
	char __sr[1<<21],__z[20];int __C=-1,__zz=0;
	inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
	inline void print(register int x)
	{
		if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
		while(__z[++__zz]=x%10+48,x/=10);
		while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
	}
	void file()
	{
		#ifndef ONLINE_JUDGE
		freopen("a.in","r",stdin);
		#endif
	}
	inline void chktime()
	{
		#ifndef ONLINE_JUDGE
		cout<<(clock()-t)/1000.0<<'\n';
		#endif
	}
	#ifdef mod
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
	ll inv(ll x){return ksm(x,mod-2);}
	#else
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
	#endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

int n;
int fa[sz];
ll sum[sz];
int s[sz];
ll dp[sz];

int main()
{
	file();
	read(n);
	rep(i,1,n) read(sum[i]);
	rep(i,2,n) read(fa[i]);
	drep(i,n,2) sum[fa[i]]+=sum[i];
	rep(i,1,n) for (ll j=sum[1]/__gcd(sum[1],sum[i]),k=j;k<=n;k+=j) ++s[k];
	ll ans=0;
	rep(i,1,n) if (i==s[i])
	{
		if (i!=1) dp[i]++;ans+=dp[i];
		for (int j=i+i;j<=n;j+=i) dp[j]+=dp[i];
	}
	cout<<ans+1;
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
typedef long long int64;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e6+5, P = 1e9+7;
int64 gcd(int64 a, int64 b) { return b?gcd(b, a%b):a; }
int n, a[N], p[N], f[N], c[N], ans;
int64 s[N];
int main()
{
    n = read(), f[1] = 1;
    for(int i = 1; i <= n; ++i) s[i] = read();
    for(int i = 2; i <= n; ++i) p[i] = read();
    for(int i = n; i > 1; --i) s[p[i]] += s[i];
    for(int i = n; i; --i) if((s[i] = s[1]/gcd(s[1], s[i])) <= n) ++c[s[i]];
    for(int i = n; i; --i) for(int j = i<<1; j <= n; j += i) c[j] += c[i];
    for(int i = 1; i <= n; ++i) if(c[i] == i) for(int j = i<<1; j <= n; j += i) f[j] = (f[j]+f[i])%P;
    for(int i = 1; i <= n; ++i) if(c[i] == i) ans = (ans+f[i])%P;
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>

typedef long long LL;
const int N = 1e6 + 10;
const LL MOD = 1e9 + 7;

int n, fa[N];
LL s[N], f[N], F[N];
// f : 鏈夊灏戜釜鐐规弧瓒砈_x = 0 (mod S/i)

LL gcd(LL x, LL y) { return !y?x:gcd(y, x%y); }

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) scanf("%d", &s[i]);
	for (int i = 2; i <= n; ++i) {
		scanf("%d", &fa[i]);
	}
	for (int i = n; i; --i) s[fa[i]] += s[i];
	for (int i = n; i; --i) {
		LL x = s[1] / gcd(s[1], s[i]);
		if (x <= n)
			++f[x];
	}

	for (int i = n; i; --i) {
		for (int j = i; (j+=i) <= n; ) f[j] += f[i];
	}
	F[1] = 1;
	LL ans = 0;
	for (int i = 1; i <= n; ++i) {
		if (f[i] == i) {
			ans = (ans + F[i]) % MOD;
			for (int j = i; (j+=i) <= n; ) F[j] = (F[j] + F[i]) % MOD;
		}
	}
	printf("%lld\n", ans);
	return 0;
}


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

const int MAXN = 1000005, MOD = 1000000007;
int par[MAXN], cnt[MAXN], n;
ll sz[MAXN], f[MAXN];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%lld", sz + i);
	for (int i = 2; i <= n; i++) scanf("%d", par + i);
	for (int i = n; i > 0; i--) sz[par[i]] += sz[i];
	ll s = sz[1];
	for (int i = 1; i <= n; i++) {
		ll p = s / __gcd(s, sz[i]);
		if (p <= n) ++cnt[p];
	}
	for (int i = n; i; i--)
	for (int j = i + i; j <= n; j += i)
		cnt[j] += cnt[i];
	f[1] = 1;
	ll res = 0;
	for (int i = 1; i <= n; i++) {
		f[i] %= MOD, res += f[i];
		for (int j = i + i; j <= n; j += i)
			if (cnt[j] == j) f[j] += f[i];
	}
	printf("%lld\n", res % MOD);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int gi() {
	char ch='!'; int res=0, fl=0;
	while(!isdigit(ch))  { ch=getchar(); if(ch == '-') fl=1; }
	while(isdigit(ch)) res=res*10+ch-'0', ch=getchar();
	return fl?-res:res;
}
typedef long long LL;
const int N=1e6+100,mod=1e9+7;
int n,a[N],fa[N],f[N],g[N],cnt[N],ans;
LL sum,s[N];
LL gcd(LL x,LL y) { return y?gcd(y,x%y):x; }
int main() {
	n=gi();
	for(int i=1;i<=n;++i)
		a[i]=gi(), sum += a[i], s[i]=a[i];
	for(int i=2;i<=n;++i)
		fa[i]=gi();
	for(int i=n;i>=1;--i)
		s[fa[i]] += s[i];
	for(int i=1;i<=n;++i) {
		LL d=sum/gcd(s[i],sum);
		if(d <= n) cnt[d] += 1;
	}
	for(int i=1;i<=n;++i)
		for(int j=i;j<=n;j += i)
			f[j]=(f[j]+cnt[i])%mod;
	for(int i=1;i<=n;++i) f[i]=(f[i] == i);
	g[1]=1;
	for(int i=1;i<=n;++i) {
		ans=(ans+g[i])%mod;
		for(int j=i+i;j<=n;j += i)
			if(f[j]) g[j]=(g[j]+g[i])%mod;
	}
	printf("%d",ans);
	return 0;
}


#include <cstdio>
#include <cctype>
typedef long long ll;

template <typename _Tp> inline void read(_Tp&x){
	char c11=getchar();x=0;while(!isdigit(c11))c11=getchar();
	while(isdigit(c11))x=x*10+c11-'0',c11=getchar();
}

const int N=1001000,p=1e9+7;
int f[N],F[N],d[N];
int n,ans;
ll a[N];

inline void pls(int&A,int B){A=A+B<p?A+B:A+B-p;}
inline ll gcd(ll A,ll B){return B?gcd(B,A%B):A;}

int main(){
	read(n);
	for(int i=1;i<=n;++i)read(a[i]);
	for(int i=2;i<=n;++i)read(d[i]);
	for(int i=n;i;--i)a[d[i]]+=a[i];
	for(int i=1;i<=n;++i){
		ll t=a[1]/gcd(a[1],a[i]);
		if(t<=n)++f[t];
	}
	for(int i=n;i;--i)
		for(int j=i+i;j<=n;j+=i)f[j]+=f[i];
	F[1]=1;
	for(int i=1;i<=n;++i)
		if(f[i]==i){
			pls(ans,F[i]);
			for(int j=i+i;j<=n;j+=i)
				pls(F[j],F[i]);
		}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxd=1e9+7;
int fa[1001000],n;
long long dp[1001000],w[1001000],sum[1001000];

int gcd(int x,int y)
{
    if (y==0) return x;
    else return gcd(y,x%y);
}

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*10+(ch-'0');ch=getchar();}
    return x*f;
}

int main()
{
    n=read();int i,j;
    long long ans=0;
    memset(dp,0,sizeof(dp));
    for (i=1;i<=n;i++) w[i]=read();
    for (i=2;i<=n;i++) fa[i]=read();
    for (i=n;i>=1;i--) w[fa[i]]+=w[i];
    for (i=n;i>=1;i--)
    {
        w[i]=w[1]/gcd(w[1],w[i]);
        if (w[i]<=n) dp[w[i]]++;
    }
    for (i=n;i>=1;i--)
        for (j=2*i;j<=n;j+=i) dp[j]=(dp[j]+dp[i])%maxd;
    sum[1]=1;
    for (i=1;i<=n;i++)
        if (dp[i]>=i)
        for (j=i*2;j<=n;j+=i) sum[j]=(sum[j]+sum[i])%maxd;
    for (i=1;i<=n;i++) if (dp[i]>=i) ans=(ans+sum[i])%maxd;
    printf("%I64d",ans);
    return 0;

}


#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define int long long
const int maxd=1e9+7;
int fa[1001000],n;
long long dp[1001000],w[1001000],sum[1001000];

int gcd(int x,int y)
{
    if (y==0) return x;
    else return gcd(y,x%y);
}

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*10+(ch-'0');ch=getchar();}
    return x*f;
}

signed main()
{
    n=read();int i,j;
    long long ans=0;
    memset(dp,0,sizeof(dp));
    for (i=1;i<=n;i++) w[i]=read();
    for (i=2;i<=n;i++) fa[i]=read();
    for (i=n;i>=1;i--) w[fa[i]]+=w[i];
    for (i=n;i>=1;i--)
    {
        w[i]=w[1]/gcd(w[1],w[i]);
        if (w[i]<=n) dp[w[i]]++;
    }
    for (i=n;i>=1;i--)
        for (j=2*i;j<=n;j+=i) dp[j]=(dp[j]+dp[i])%maxd;
    sum[1]=1;
    for (i=1;i<=n;i++)
        if (dp[i]>=i)
        for (j=i*2;j<=n;j+=i) sum[j]=(sum[j]+sum[i])%maxd;
    for (i=1;i<=n;i++) if (dp[i]>=i) ans=(ans+sum[i])%maxd;
    printf("%I64d",ans);
    return 0;

}


#include<bits/stdc++.h>
#define ll long long
#define mo 1000000007
using namespace std;
struct Info{int nu,ne;}a[2000010];
int n,num,b[1000010],d[1000010],nd,p[1000010],x,ansn,ans[1000010];
ll sum,c[1000010];
void jb(int x,int y){a[++num].nu=y;a[num].ne=b[x];b[x]=num;}
void dfs(int x,int fa){
	for (int y=b[x];y;y=a[y].ne)if (a[y].nu!=fa){dfs(a[y].nu,x);c[x]+=c[a[y].nu];}
}
int work(int x){
	ll y=sum/x;int z=0;
	for (int i=1;i<=n;i++) if (c[i]%y==0) z++;
	if (z>=x) return 1;return 0;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){scanf("%d",&c[i]);sum+=c[i];}
	for(int i=1;i<=n;i++){if (sum%i==0) d[++nd]=i;}
	for(int i=1;i<=nd;i++)p[i]=2;
	for(int i=2;i<=n;i++){scanf("%d",&x);jb(x,i);jb(i,x);}
	dfs(1,0);
	for(int i=1;i<=nd;i++){
		p[i]=work(d[i]);
	}
	ans[1]=1;
	for (int i=1;i<=nd;i++){
		if (p[i]==1){
			for (int j=i+1;j<=nd;j++){
				if (p[j]==1&&d[j]%d[i]==0)ans[j]=(ans[j]+ans[i])%mo;
			}
		}
	}
	for (int i=1;i<=nd;i++) if (p[i]==1) ansn=(ansn+ans[i])%mo;
	cout<<ansn<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mo 1000000007
using namespace std;
struct Info{int nu,ne;}a[2000010];
int n,num,b[1000010],d[1000010],e[1000010],x;
ll sum,c[1000010],ansn;
void jb(int x,int y){a[++num].nu=y;a[num].ne=b[x];b[x]=num;}
void dfs(int x,int fa){
	for (int y=b[x];y;y=a[y].ne)if (a[y].nu!=fa){dfs(a[y].nu,x);c[x]+=c[a[y].nu];}
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){scanf("%d",&c[i]);sum+=c[i];}
	for(int i=2;i<=n;i++){scanf("%d",&x);jb(x,i);jb(i,x);}
	dfs(1,0);
	for (int i=1;i<=n;i++){if (c[1]/__gcd(c[i],c[1])<=n)d[c[1]/__gcd(c[i],c[1])]++;}
	for (int i=n;i>=1;i--)
		for (int j=i+i;j<=n;j+=i)d[j]+=d[i];
	e[1]=1;
	for (int i=1;i<=n;i++){
		if (d[i]==i){
			for (int j=i+i;j<=n;j+=i){
				if (d[j]==j)e[j]+=e[i];
			}
			ansn+=e[i];
		}
	}
	cout<<ansn<<endl;
	return 0;
}


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

#define N 1000010
typedef long long ll;

int n, a[N];
vector<int> g[N];

ll sz[N];

void dfs(int u, int pa) {
    sz[u] = a[u];
    for (int v:g[u]) {
		if (v == pa) continue;
		dfs(v, u);
		sz[u] += sz[v];
    }
}

const int mod = 1e9 + 7;
int b[N];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", a + i);
	for (int i = 2, u; i <= n; i ++) {
        scanf("%d", &u);
        g[u].push_back(i);
        g[i].push_back(u);
	}
	dfs(1, 0);
	ll sum = sz[1];
	for (int i = 1; i <= n; i ++) {
		ll gc = __gcd(sum, sz[i]);
		if (sum / gc <= n) b[sum/gc] ++;
	}
	for (int i = n; i >= 1; i --) {
		for (int j = 2 * i; j <= n; j += i) b[j] += b[i];
	}
	for (int i = 1; i <= n; i ++) {
		assert(b[i] <= i);
		if (b[i] == i) b[i] = 1;
		else b[i] = 0;
	}
	for (int i = 2; i <= n; i ++) {
		for (int j = 2 * i; j <= n; j += i) if (b[j]) {
			b[j] += b[i];
			if (b[j] >= mod) b[j] -= mod;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		ans += b[i];
		if (ans >= mod) ans -= mod;
	}
	printf("%d\n", ans);
}


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

#define N 1000010
typedef long long ll;

int n, a[N];
vector<int> g[N];

ll sz[N];

void dfs(int u, int pa) {
    sz[u] = a[u];
    for (int v:g[u]) {
		if (v == pa) continue;
		dfs(v, u);
		sz[u] += sz[v];
    }
}

const int mod = 1e9 + 7;
int b[N];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", a + i);
	for (int i = 2, u; i <= n; i ++) {
        scanf("%d", &u);
        g[u].push_back(i);
        g[i].push_back(u);
	}
	dfs(1, 0);
	ll sum = sz[1];
	for (int i = 1; i <= n; i ++) {
		ll gc = __gcd(sum, sz[i]);
		if (sum / gc <= n) b[sum/gc] ++;
	}
	for (int i = n; i >= 1; i --) {
		for (int j = 2 * i; j <= n; j += i) b[j] += b[i];
	}
	for (int i = 1; i <= n; i ++) {
		assert(b[i] <= i);
		if (b[i] == i) b[i] = 1;
		else b[i] = 0;
	}
	for (int i = 2; i <= n; i ++) {
		for (int j = 2 * i; j <= n; j ++) if (b[j]) {
			b[j] += b[i];
			if (b[j] >= mod) b[j] -= mod;
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		ans += b[i];
		if (ans >= mod) ans -= mod;
	}
	printf("%d\n", ans);
}


#include "iostream"
#include "cstring"
#include "cstring"
#include "cstdio"
using namespace std;
#define MAXN 1000016
#define P 1000000007
typedef long long ll;
int n;
int A[MAXN] , fa[MAXN] , num[MAXN] , f[MAXN]; ll S[MAXN];
ll gcd( ll a , ll b ) { return !b ? a : gcd( b , a % b ); }
int main() {
    cin >> n;
    for( int i = 1 ; i <= n ; ++ i ) scanf("%d",&A[i]);
    for( int i = 2 ; i <= n ; ++ i ) scanf("%d",&fa[i]);
    for( int i = n ; i ; -- i ) S[i] += A[i] , S[fa[i]] += S[i];
    ll s = S[1];
    for( int i = 1 ; i <= n ; ++ i ) if( s / gcd( S[i] , s ) <= n ) ++ num[s / gcd( S[i] , s )];
    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 );
    ll ans = 0;
    for( int i = n ; i > 1 ; -- i ) if( f[i] ) {
        for (int j = i + i; j <= n; j += i) ( f[i] += f[j] ) %= P;
        ( ans += f[i] ) %= P;
    }
    cout << ans + 1 << endl;
}


#include "iostream"
#include "cstring"
#include "cstring"
#include "cstdio"
using namespace std;
#define MAXN 1000006
#define P 1000000007
typedef long long ll;
int n;
int A[MAXN] , fa[MAXN] , num[MAXN] , f[MAXN]; ll S[MAXN];
ll gcd( ll a , ll b ) { return !b ? a : gcd( b , a % b ); }
int main() {
    cin >> n;
    for( int i = 1 ; i <= n ; ++ i ) scanf("%d",&A[i]);
    for( int i = 2 ; i <= n ; ++ i ) scanf("%d",&fa[i]);
    for( int i = n ; i ; -- i ) S[i] += A[i] , S[fa[i]] += S[i];
    ll s = S[1];
    for( int i = 1 ; i <= n ; ++ i ) if( s / gcd( S[i] , s ) <= n ) ++ num[s / gcd( S[i] , s )];
    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 );
    ll ans = 0;
    for( int i = n ; i > 1 ; -- i ) if( f[i] ) {
        for (int j = i + i; j <= n; j += i) ( f[i] += f[j] ) %= P;
        ( ans += f[i] ) %= P;
    }
    cout << ans + 1 << endl;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ans,zo,n,f[1000010],a[1000010],g[1000010],fa[1000010],p=1e9+7;
ll gcd(ll x,ll y){
	if(!y)return x;
	return gcd(y,x%y);
}
int main(){
	ll i,x,j;
	scanf("%lld",&n);
	for(i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(i=1;i<=n;i++)zo+=a[i];
	for(i=2;i<=n;i++){
		scanf("%lld",&fa[i]);
	}
	for(i=n;i>1;i--)a[fa[i]]+=a[i];
	for(i=1;i<=n;i++){
		x=zo/gcd(zo,a[i]);
		//printf("%lld %lld\n",a[i],x);
		if(x>n)continue;
		g[x]++;
	}
	for(i=n;i;i--)
	 for(j=i*2;j<=n;j+=i)(g[j]+=g[i])%=p;
	f[1]=1;
	for(i=1;i<=n;i++)if(g[i]==i){
		ans=(ans+f[i])%p;
		for(j=i*2;j<=n;j+=i)f[j]=(f[j]+f[i])%p;
	}
	printf("%lld",ans);
}
/*
一道大水题，怎么就这么点人过啊
*/


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef pair<int, int> pii;
typedef double lf;
typedef long long ll;
const int inf=0x3f3f3f3f, mod=1000000007;
#define upd(a,b) (a+=b,(a>=mod?a-=mod:0))
ll val[2000100], fa[2000100], f[2000100], ans[2000100];

inline ll read()
{
    ll x=0,f=1; char ch=getchar();
    for (; ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') f=-1;
    for (; ch>='0' && ch<='9'; ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}

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

int main()
{
    ll n=read(), res=0;
    for (int i=1; i<=n; i++) val[i]=read();
    for (int i=2; i<=n; i++) fa[i]=read();
    for (int i=n; i; i--) val[fa[i]]+=val[i];
    for (int i=n; i; i--) val[i]=val[1]/gcd(val[1], val[i]);
    for (int i=1; i<=n; i++) if (val[i]<=n) f[val[i]]++;
    for (int i=n; i; i--)
        for (int j=2*i; j<=n; j+=i) upd(f[j], f[i]);
    for (int i=ans[1]=1; i<=n; i++)
        if (f[i]>=i)
            for (int j=2*i; j<=n; j+=i)
                upd(ans[j], ans[i]);
    for (int i=1; i<=n; i++) if (f[i]>=i) upd(res, ans[i]);
    printf("%I64d\n",res);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef pair<int, int> pii;
typedef double lf;
typedef long long ll;
const int inf=0x3f3f3f3f, mod=1000000007;
#define upd(a,b) (a+=b,(a>=mod?a-=mod:0))
ll val[2000100], fa[2000100], f[2000100], ans[2000100];

inline ll read()
{
    ll x=0,f=1; char ch=getchar();
    for (; ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') f=-1;
    for (; ch>='0' && ch<='9'; ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}

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

int main()
{
    ll n=read(), res=0;
    for (int i=1; i<=n; i++) val[i]=read();
    for (int i=2; i<=n; i++) fa[i]=read();
    for (int i=n; i; i--) val[fa[i]]+=val[i];
    for (int i=n; i; i--) val[i]=val[1]/gcd(val[1], val[i]);
    for (int i=1; i<=n; i++) if (val[i]<=n) f[val[i]]++;
    for (int i=n; i; i--)
        for (int j=2*i; j<=n; j+=i) upd(f[j], f[i]);
    for (int i=ans[1]=1; i<=n; i++)
        if (f[i]>=i)
            for (int j=2*i; j<=n; j+=i)
                upd(ans[j], ans[i]);
    for (int i=1; i<=n; i++) if (f[i]>=i) upd(res, ans[i]);
    printf("%I64d\n",res);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef pair<int, int> pii;
typedef double lf;
typedef long long ll;
const int inf=0x3f3f3f3f, mod=1000000007;
ll val[1000100], fa[1000100], f[1000100], ans[1000100];

inline ll read()
{
    int x=0,f=1; char ch=getchar();
    for (; ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') f=-1;
    for (; ch>='0' && ch<='9'; ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}

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

int main()
{
    ll n=read(), res=0;
    for (int i=1; i<=n; i++) val[i]=read();
    for (int i=2; i<=n; i++) fa[i]=read();
    for (int i=n; i; i--) val[fa[i]]+=val[i];
    for (int i=n; i; i--) val[i]=val[1]/gcd(val[1], val[i]);
    for (int i=1; i<=n; i++) if (val[i]<=n) f[val[i]]++;
    for (int i=n; i; i--)
        for (int j=2*i; j<=n; j+=i) f[j]=(f[j]+f[i])%mod;
    for (int i=ans[1]=1; i<=n; i++)
        if (f[i]>=i)
            for (int j=2*i; j<=n; j+=i)
                ans[j]=(ans[j]+ans[i])%mod;
    for (int i=1; i<=n; i++) if (f[i]>=i) res=(res+ans[i])%mod;
    printf("%I64d\n",res);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef pair<int, int> pii;
typedef double lf;
typedef long long ll;
const int inf=0x3f3f3f3f, mod=1000000007;
ll val[1000100], fa[1000100], f[1000100], ans[1000100];

inline ll read()
{
    ll x=0,f=1; char ch=getchar();
    for (; ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') f=-1;
    for (; ch>='0' && ch<='9'; ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}

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

int main()
{
    ll n=read(), res=0;
    for (int i=1; i<=n; i++) val[i]=read();
    for (int i=2; i<=n; i++) fa[i]=read();
    for (int i=n; i; i--) val[fa[i]]+=val[i];
    for (int i=n; i; i--) val[i]=val[1]/gcd(val[1], val[i]);
    for (int i=1; i<=n; i++) if (val[i]<=n) f[val[i]]++;
    for (int i=n; i; i--)
        for (int j=2*i; j<=n; j+=i) f[j]=(f[j]+f[i])%mod;
    for (int i=ans[1]=1; i<=n; i++)
        if (f[i]>=i)
            for (int j=2*i; j<=n; j+=i)
                ans[j]=(ans[j]+ans[i])%mod;
    for (int i=1; i<=n; i++) if (f[i]>=i) res=(res+ans[i])%mod;
    printf("%I64d\n",res);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef pair<int, int> pii;
typedef double lf;
typedef long long ll;
const int inf=0x3f3f3f3f, mod=1000000007;
ll val[2000100], fa[2000100], f[2000100], ans[2000100];

inline ll read()
{
    ll x=0,f=1; char ch=getchar();
    for (; ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') f=-1;
    for (; ch>='0' && ch<='9'; ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}

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

int main()
{
    ll n=read(), res=0;
    for (int i=1; i<=n; i++) val[i]=read();
    for (int i=2; i<=n; i++) fa[i]=read();
    for (int i=n; i; i--) val[fa[i]]+=val[i];
    for (int i=n; i; i--) val[i]=val[1]/gcd(val[1], val[i]);
    for (int i=1; i<=n; i++) if (val[i]<=n) f[val[i]]++;
    for (int i=n; i; i--)
        for (int j=2*i; j<=n; j+=i) f[j]=(f[j]+f[i])%mod;
    for (int i=ans[1]=1; i<=n; i++)
        if (f[i]>=i)
            for (int j=2*i; j<=n; j+=i)
                ans[j]=(ans[j]+ans[i])%mod;
    for (int i=1; i<=n; i++) if (f[i]>=i) res=(res+ans[i])%mod;
    printf("%I64d\n",res);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef pair<int, int> pii;
typedef double lf;
typedef long long ll;
const int inf=0x3f3f3f3f, mod=1000000007;
#define upd(a,b) (a+=b,(a>=mod?a-=mod:0))
ll val[2000100], fa[2000100], f[2000100], ans[2000100];

inline ll read()
{
    ll x=0,f=1; char ch=getchar();
    for (; ch<'0' || ch>'9'; ch=getchar()) if (ch=='-') f=-1;
    for (; ch>='0' && ch<='9'; ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}

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

int main()
{
    ll n=read(), res=0;
    for (int i=1; i<=n; i++) val[i]=read();
    for (int i=2; i<=n; i++) fa[i]=read();
    for (int i=n; i; i--) val[fa[i]]+=val[i];
    for (int i=n; i; i--) val[i]=val[1]/gcd(val[1], val[i]);
    for (int i=1; i<=n; i++) if (val[i]<=n) f[val[i]]++;
    for (int i=n; i; i--)
        for (int j=2*i; j<=n; j+=i) upd(f[j], f[i]);
    for (int i=ans[1]=1; i<=n; i++)
        if (f[i]>=i)
            for (int j=2*i; j<=n; j+=i)
                upd(ans[j], ans[i]);
    for (int i=1; i<=n; i++) if (f[i]>=i) upd(res, ans[i]);
    printf("%I64d\n",res);
    return 0;//chj tcl
}


#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const ll mod=(1e9)+7;
const int maxn=(1e6)+10;
int n,fa[maxn];
ll S,s[maxn],s1[maxn],s2[maxn],dp[maxn],tmp,ans;
ll gcd(ll x,ll y) {
	if (!x||!y) return x+y;
	return gcd(y,x%y);
}
void update(ll &x,ll y) {
	x+=y; if (x>=mod) x-=mod;
}
int main() {
	//freopen("1.txt","r",stdin);
	read(n);
	for (int i=1;i<=n;i++) read(s[i]),S+=s[i];
	for (int i=2;i<=n;i++) read(fa[i]);
	for (int i=n;i>=2;i--) s[fa[i]]+=s[i];
	for (int i=1;i<=n;i++) {
		tmp=S/gcd(S,s[i]);
		if (tmp<=n) s1[tmp]++;
	}
	for (int i=1;i<=n;i++)
	for (int j=i;j<=n;j+=i) s2[j]+=s1[i];
	dp[1]=1;
	for (int i=1;i<=n;i++)
		if (s2[i]==i) {
			update(ans,dp[i]);
			for (int j=i*2;j<=n;j+=i) update(dp[j],dp[i]);
		}
	printf("%lld\n",ans);
	return 0;
}
/*
  REMEMBER:
  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/


#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
const int inf=1e9;
const ll Inf=1e18;
const int N=1e6+10;
const int mod=1e9+7;
int gi() {
    int x=0,o=1;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
template<typename T> bool chkmax(T &a,T b) { return a<b?a=b,1:0; };
template<typename T> bool chkmin(T &a,T b) { return a>b?a=b,1:0; };
int add(int a,int b) { return a+b>=mod?a+b-mod:a+b; }
int sub(int a,int b) { return a-b<0?a-b+mod:a-b; }
void inc(int &a,int b) { a=(a+b>=mod?a+b-mod:a+b); }
void dec(int &a,int b) { a=(a-b<0?a-b+mod:a-b); }
int n,p[N],g[N],f[N],ans=0;
ll a[N];
int main() {
#ifndef ONLINE_JUDGE
    freopen("CF1034C.in","r",stdin);
    freopen("CF1034C.out","w",stdout);
#endif
    cin>>n;
    for(int i=1;i<=n;i++) a[i]=gi();
    for(int i=2;i<=n;i++) p[i]=gi();
    for(int i=n;i>1;i--) a[p[i]]+=a[i];
    for(int i=1;i<=n;i++) {
        int tmp=a[1]/__gcd(a[1],a[i]);
        if(tmp<=n) g[tmp]++;
    }
    for(int i=1;i<=n;i++)
        for(int j=i+i;j<=n;j+=i) g[j]=(g[j]+g[i])%mod;
    f[1]=1;
    for(int i=1;i<=n;i++)
        if(g[i]>=i) {
            ans=(ans+f[i])%mod;
            for(int j=i+i;j<=n;j+=i) f[j]=(f[j]+f[i])%mod;
        }
    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
const int inf=1e9;
const ll Inf=1e18;
const int N=1e6+10;
const int mod=1e9+7;
int gi() {
    int x=0,o=1;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
template<typename T> bool chkmax(T &a,T b) { return a<b?a=b,1:0; };
template<typename T> bool chkmin(T &a,T b) { return a>b?a=b,1:0; };
int add(int a,int b) { return a+b>=mod?a+b-mod:a+b; }
int sub(int a,int b) { return a-b<0?a-b+mod:a-b; }
void inc(int &a,int b) { a=(a+b>=mod?a+b-mod:a+b); }
void dec(int &a,int b) { a=(a-b<0?a-b+mod:a-b); }
int n,p[N],g[N],f[N],ans=0;
ll a[N];
int main() {
#ifndef ONLINE_JUDGE
    freopen("CF1034C.in","r",stdin);
    freopen("CF1034C.out","w",stdout);
#endif
    cin>>n;
    for(int i=1;i<=n;i++) a[i]=gi();
    for(int i=2;i<=n;i++) p[i]=gi();
    for(int i=n;i>1;i--) a[p[i]]+=a[i];
    for(int i=1;i<=n;i++) {
        ll tmp=a[1]/__gcd(a[1],a[i]);
        if(tmp<=n) g[tmp]++;
    }
    for(int i=n;i;i--)
        for(int j=i+i;j<=n;j+=i) g[j]=(g[j]+g[i])%mod;
    f[1]=1;
    for(int i=1;i<=n;i++)
        if(g[i]==i) {
            ans=(ans+f[i])%mod;
            for(int j=i+i;j<=n;j+=i) f[j]=(f[j]+f[i])%mod;
        }
    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int M=1e6+5,mod=1e9+7;
long long val[M];
int dad[M],dp[M],cot[M],ans,n,i,j;
void in(){scanf("%d",&n);for(int i=1;i<=n;++i)scanf("%d",&val[i]);for(int i=2;i<=n;++i)scanf("%d",&dad[i]);}
void ac()
{
	for(i=n;i>1;--i)val[dad[i]]+=val[i];
	for(i=n;i;--i)if((val[i]=val[1]/__gcd(val[1],val[i]))<=n)++cot[val[i]];
	for(i=n;i;--i)for(j=i;(j+=i)<=n;)(cot[j]+=cot[i])%=mod;
	for(dp[1]=i=1;i<=n;++i)if(cot[i]==i)for((ans+=dp[j=i])%=mod;(j+=i)<=n;)(dp[j]+=dp[i])%=mod;
	printf("%d",ans);
}
int main(){in();ac();}


#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == --x.end() ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#ifdef DEBUG
  const int seed = 1;
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (#x != "" ? #x ":  " : ""), dump(x), Nl(), cerr
#else
  const int seed = chrono::system_clock::now().time_since_epoch().count();
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(seed);
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}

vector<int> a;
vector<LL> sub;
vector<vector<int>> graph;

int t = -1;
void dfs(int v = 0) {
	sub[v] = a[v];
	for(int u : graph[v]) {
		dfs(u);
		sub[v] += sub[u];
	}
}

LL check(LL d, int v = 0)
{
	LL sum = 0;
	for(int u : graph[v])
		sum += check(d, u);
	if(sub[v] % d == 0 && sum + 1 == sub[v] / d) sum++;
	else if(v == 0) return 0;
	return sum;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	LL sum = 0;
	a.resize(n);
	REP(i, n) {
		cin >> a[i];
		sum += a[i];
	}

	graph.resize(n);
	FOR(i, 1, n - 1) {
		int p;
		cin >> p;
		graph[p - 1].emplace_back(i);
	}

	sub.resize(n);
	dfs();

	vector<int> ord(n);
	REP(i, n) ord[i] = i;
	sort(ord.begin(), ord.end(), [&](int i, int j) { return sub[i] < sub[j]; });

	int mod = 1e9 + 7;
	vector<int> ans(n);
	REP(i, n) {
		int v = ord[i];
		if(i != 0 && sub[v] == sub[ord[i - 1]]) continue;
		if(sum % sub[v] == 0 && sum / sub[v] <= n && check(sub[v])) {
			ans[i] = 1;
			FOR(j, 0, i - 1) {
				if(sub[v] % sub[ord[j]] == 0) {
					ans[i] += ans[j];
					if(ans[i] > mod) ans[i] -= mod;
				}
			}
		}
	}

	cout << ans[n - 1] << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == --x.end() ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#ifdef DEBUG
  const int seed = 1;
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (#x != "" ? #x ":  " : ""), dump(x), Nl(), cerr
#else
  const int seed = chrono::system_clock::now().time_since_epoch().count();
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(seed);
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}

vector<int> a;
vector<LL> sub;
vector<vector<int>> graph;

int t = -1;
void dfs(int v = 0) {
	sub[v] = a[v];
	for(int u : graph[v]) {
		dfs(u);
		sub[v] += sub[u];
	}
}

LL check(LL d, int v = 0)
{
	LL sum = 0;
	for(int u : graph[v])
		sum += check(d, u);
	if(sub[v] % d == 0 && sum + 1 == sub[v] / d) sum++;
	else if(v == 0) return 0;
	return sum;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	LL sum = 0;
	a.resize(n);
	REP(i, n) {
		cin >> a[i];
		sum += a[i];
	}

	graph.resize(n);
	FOR(i, 1, n - 1) {
		int p;
		cin >> p;
		graph[p - 1].emplace_back(i);
	}

	sub.resize(n);
	dfs();

	vector<int> ord(n);
	REP(i, n) ord[i] = i;
	sort(ord.begin(), ord.end(), [&](int i, int j) { return sub[i] < sub[j]; });

	int mod = 1e9 + 7;
	vector<int> ans(n);
	REP(i, n) {
		int v = ord[i];
		if(i != 0 && sub[v] == sub[ord[i - 1]]) continue;
		if(sum % sub[v] == 0 && check(sub[v])) {
			ans[i] = 1;
			FOR(j, 0, i - 1) {
				if(sub[v] % sub[ord[j]] == 0) {
					ans[i] += ans[j];
					if(ans[i] > mod) ans[i] -= mod;
				}
			}
		}
	}

	cout << ans[n - 1] << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == --x.end() ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#ifdef DEBUG
  const int seed = 1;
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (#x != "" ? #x ":  " : ""), dump(x), Nl(), cerr
#else
  const int seed = chrono::system_clock::now().time_since_epoch().count();
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(seed);
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	REP(i, n)
		cin >> a[i];

	vector<int> p(n);
	FOR(i, 1, n - 1)
		cin >> p[i];

	vector<LL> sum(n);
	for(int i = n - 1; i >= 0; i--) {
		sum[i] += a[i];
		if(i) sum[p[i] - 1] += sum[i];
	}

	vector<int> k(n + 1);
	REP(i, n) {
		// s[i] % (S / (q * x)) == 0
		LL q = sum[0] / __gcd(sum[0], sum[i]);
		if(q <= n) k[q]++;
		debug(i, sum[i], q);
	}

	vector<int> cnt(n + 1);
	FOR(i, 1, n) if(k[i]) {
		for(int j = i; j <= n; j += i)
			cnt[j] += k[i];
	}

	vector<bool> good_split(n + 1);
	FOR(i, 1, n)
	{
		if(cnt[i] == i) good_split[i] = true;
		debug(i, cnt[i], good_split[i]);
	}

	vector<int> ans(n + 1);
	for(int i = n; i >= 1; i--)
	{
		if(!good_split[i]) continue;
		ans[i] = 1;
		for(int j = i * 2; j <= n; j += i)
			ans[i] += ans[j];
	}

	cout << ans[1]  << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == --x.end() ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#ifdef DEBUG
  const int seed = 1;
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (#x != "" ? #x ":  " : ""), dump(x), Nl(), cerr
#else
  const int seed = chrono::system_clock::now().time_since_epoch().count();
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(seed);
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}

struct Tree {
	using T = int;
	T f(T a, T b) { return a + b; }
	vector<T> nodes;
	int size = 1;

	Tree(int n, T val = 0) {
		while(size < n) size *= 2;
		nodes.resize(size * 2, val);
	}

	void update(int pos, T val) {
		nodes[pos += size] = val;
		while(pos /= 2)
			nodes[pos] = f(nodes[pos * 2], nodes[pos * 2 + 1]);
	}

	T query(int l, int r) {
		l += size, r += size;
		T ret = (l != r ? f(nodes[l], nodes[r]) : nodes[l]);
		while(l + 1 < r) {
			if(l % 2 == 0) ret = f(ret, nodes[l + 1]);
			if(r % 2 == 1) ret = f(ret, nodes[r - 1]);
			l /= 2, r /= 2;
		}
		return ret;
	}
};

vector<int> a;
vector<LL> sub;
vector<vector<int>> graph;
vector<int> pre, pos, par;

int t = -1;
void dfs(int v = 0) {
	sub[v] = a[v];
	pre[v] = ++t;
	for(int u : graph[v]) {
		dfs(u);
		sub[v] += sub[u];
	}
	pos[v] = t;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	LL sum = 0;
	a.resize(n);
	REP(i, n) {
		cin >> a[i];
		sum += a[i];
	}

	graph.resize(n);
	par.resize(n);
	FOR(i, 1, n - 1) {
		cin >> par[i];
		par[i]--;
		graph[par[i]].emplace_back(i);
	}

	sub.resize(n);
	pre.resize(n);
	pos.resize(n);
	dfs();

	vector<int> ord(n);
	REP(i, n) ord[i] = i;
	sort(ord.begin(), ord.end(), [&](int i, int j) { return sub[i] < sub[j]; });

	auto check = [&](LL d) -> bool {
		Tree tree(n);
		REP(i, n) {
			int v = ord[i];
			if(sub[v] % d == 0 && tree.query(pre[v], pos[v]) + 1 == sub[v] / d)
				tree.update(pre[v], 1);
		}
		return tree.query(pre[0], pre[0]);
	};

	int mod = 1e9 + 7;
	vector<int> ans(n);
	REP(i, n) {
		int v = ord[i];
		if(i != 0 && sub[v] == sub[ord[i - 1]]) continue;
		if(sum % sub[v] == 0 && check(sub[v])) {
			ans[i] = 1;
			FOR(j, 0, i - 1)
				if(sub[v] % sub[ord[j]] == 0)
					ans[i] += ans[j];
		}
	}

	cout << ans[n - 1] << "\n";
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int sz=1e6+10, mod=1e9+7;
vector <int> sv[sz];
int va[sz], q[sz], q2[sz], dp[sz];
void dfs(int v)
{
    for(int a=0; a<sv[v].size(); a++)
    {
        int ne=sv[v][a];
        dfs(ne), va[v]+=va[ne];
    }
}
main()
{
    //freopen("input.txt", "r", stdin);
    int n;
    cin>>n;
    for(int a=0; a<n; a++) scanf("%lld", &va[a]);
    for(int a=1; a<n; a++)
    {
        int pr;
        scanf("%lld", &pr);
        sv[pr-1].push_back(a);
    }
    dfs(0);
    for(int a=0; a<n; a++)
    {
        int k=va[0]/__gcd(va[a], va[0]);
        if(k<=n) q[k]++;
    }
    for(int a=1; a<=n; a++)
    {
        for(int b=a; b<=n; b+=a) if(va[0]%b==0) q2[b]+=q[a];
    }
    for(int a=1; a<=n; a++)
    {
        if(q2[a]==a) dp[a]=1;
        else dp[a]=0;
    }
    int an=0;
    for(int a=n; a>=1; a--)
    {
        if(dp[a])
        {
            for(int b=a+a; b<=n; b+=a) dp[a]=(dp[a]+dp[b])%mod;
        }
    }
    cout<<dp[1];
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int sz=1e6+10, mod=1e9+7;
vector <int> sv[sz];
int va[sz], q[sz], q2[sz], dp[sz];
void dfs(int v)
{
    for(int a=0; a<sv[v].size(); a++)
    {
        int ne=sv[v][a];
        dfs(ne), va[v]+=va[ne];
    }
}
main()
{
    //freopen("input.txt", "r", stdin);
    int n;
    cin>>n;
    for(int a=0; a<n; a++) scanf("%lld", &va[a]);
    for(int a=1; a<n; a++)
    {
        int pr;
        scanf("%lld", &pr);
        sv[pr-1].push_back(a);
    }
    dfs(0);
    for(int a=0; a<n; a++)
    {
        int k=va[0]/__gcd(va[a], va[0]);
        if(k<=n) q[k]++;
    }
    for(int a=1; a<=n; a++)
    {
        for(int b=a; b<=n; b+=a) if(va[0]%b==0) q2[b]+=q[a];
    }
    for(int a=1; a<=n; a++)
    {
        if(q2[a]==a) dp[a]=1;
        else dp[a]=0;
    }
    int an=0;
    for(int a=n; a>=1; a--)
    {
        if(dp[a])
        {
            for(int b=a+a; b<=n; b++) dp[a]=(dp[a]+dp[b])%mod;
        }
    }
    cout<<dp[1];
}


#include <bits/stdc++.h>

using namespace std;

const int max_n = 1000111, mod = 1000000007;

int n, a[max_n], p[max_n], sz[max_n];
int f[max_n], dp[max_n];
long long x[max_n];
int qa, qb, q[max_n];

bool check(long long s) {
    for (int i = 1; i <= n; ++i) {
        sz[i] = 0;
        x[i] = a[i];
        ++sz[p[i]];
    }
    qa = 0;
    qb = 0;
    for (int i = 1; i <= n; ++i) {
        if (sz[i] == 0) {
            q[qb++] = i;
        }
    }
    while (qa < qb) {
        int v = q[qa++];
        if (x[v] > s) {
            return false;
        }
        --sz[p[v]];
        if (x[v] < s) {
            x[p[v]] += x[v];
            if (x[p[v]] > s) {
                return false;
            }
        }
        if (sz[p[v]] == 0) {
            q[qb++] = p[v];
        }
    }
    return true;
}

void add(int &x, int y) {
    x += y;
    if (x >= mod) {
        x -= mod;
    }
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    if (1) {
        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]);
        }
    } else {
        n = 720720;
        for (int i = 1; i <= n; ++i) {
            a[i] = 93248932;
            p[i] = i - 1;
        }
    }
    long long sum = 0;
    for (int i = 1; i <= n; ++i) {
        sum += a[i];
    }
    for (int cnt = 1; cnt <= n; ++cnt) {
        if (sum % cnt == 0) {
            long long s = sum / cnt;
            if (check(s)) {
                f[cnt] = 1;
            }
        }
    }
    dp[1] = 1;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        add(ans, dp[i]);
        for (int j = 2 * i; j <= n; j += i) {
            if (f[j]) {
                add(dp[j], dp[i]);
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int max_n = 1000111, mod = 1000000007;

int n, a[max_n], p[max_n];
int cnt[max_n], tot[max_n], f[max_n], dp[max_n];
long long sum[max_n];

void add(int &x, int y) {
    x += y;
    if (x >= mod) {
        x -= mod;
    }
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        sum[i] = a[i];
    }
    for (int i = 2; i <= n; ++i) {
        scanf("%d", &p[i]);
    }
    for (int i = n; i >= 1; --i) {
        sum[p[i]] += sum[i];
    }
    for (int i = 1; i <= n; ++i) {
        long long g = __gcd(sum[1], sum[i]);
        long long s = sum[1] / g;
        if (s <= n) {
            ++cnt[s];
        }
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = i; j <= n; j += i) {
            tot[j] += cnt[i];
        }
    }
    for (int cnt = 1; cnt <= n; ++cnt) {
        if (sum[1] % cnt == 0 && tot[cnt] == cnt) {
            f[cnt] = 1;
        }
    }
    dp[1] = 1;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        add(ans, dp[i]);
        for (int j = 2 * i; j <= n; j += i) {
            if (f[j]) {
                add(dp[j], dp[i]);
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int N=1000005;
const int MOD=1e9+7;
int gcd (int x,int y)	{return x==0?y:gcd(y%x,x);}
int fa[N],siz[N];
int n;
int f[N];
int g[N];
int main()
{
	scanf("%d",&n);
	for (int u=1;u<=n;u++) scanf("%d",&siz[u]);
	for (int u=2;u<=n;u++) scanf("%d",&fa[u]);
	for (int u=n;u>=2;u--)	siz[fa[u]]=siz[fa[u]]+siz[u];
	for (int u=1;u<=n;u++)
	{
		int t=siz[1]/gcd(siz[1],siz[u]);
	//	printf("YES:%d\n",siz[u]);
		if (t<=n) f[t]++;
	}
	for (int u=1;u<=n;u++)
	{
		for (int i=u;i<=n;i+=u)
			g[i]=g[i]+f[u];
		f[u]=0;
	}
	int ans=0;
	f[1]=1;
	for (int u=1;u<=n;u++)
	{
	//	printf("YES:%d %d\n",u,g[u]);
		if (siz[1]%u==0&&g[u]==u)
		{
			ans=(ans+f[u])%MOD;
			for (int i=u+u;i<=n;i+=u) f[i]=(f[i]+f[u])%MOD;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const LL N=1000005;
const LL MOD=1e9+7;
LL gcd (LL x,LL y)	{return x==0?y:gcd(y%x,x);}
LL fa[N],siz[N];
LL n;
LL f[N];
LL g[N];
int main()
{
	scanf("%I64d",&n);
	for (LL u=1;u<=n;u++) scanf("%I64d",&siz[u]);
	for (LL u=2;u<=n;u++) scanf("%I64d",&fa[u]);
	for (LL u=n;u>=2;u--)	siz[fa[u]]=siz[fa[u]]+siz[u];
	for (LL u=1;u<=n;u++)
	{
		LL t=siz[1]/gcd(siz[1],siz[u]);
	//	printf("YES:%lld\n",siz[u]);
		if (t<=n) f[t]++;
	}
	for (LL u=1;u<=n;u++)
	{
		for (LL i=u;i<=n;i+=u)
			g[i]=g[i]+f[u];
		f[u]=0;
	}
	LL ans=0;
	f[1]=1;
	for (LL u=1;u<=n;u++)
	{
	//	printf("YES:%lld %lld\n",u,g[u]);
		if (siz[1]%u==0&&g[u]==u)
		{
			ans=(ans+f[u])%MOD;
			for (LL i=u+u;i<=n;i+=u) f[i]=(f[i]+f[u])%MOD;
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=1000005;
const int MOD=1e9+7;
LL gcd (LL x,LL y)	{return x==0?y:gcd(y%x,x);}
int fa[N],siz[N];
int n;
LL f[N];
int g[N];
int main()
{
	scanf("%d",&n);
	for (int u=1;u<=n;u++) scanf("%d",&siz[u]);
	for (int u=2;u<=n;u++) scanf("%d",&fa[u]);
	for (int u=n;u>=2;u--)	siz[fa[u]]=siz[fa[u]]+siz[u];
	for (int u=1;u<=n;u++)
	{
		LL t=siz[1]/gcd(siz[1],siz[u]);
	//	printf("YES:%d\n",siz[u]);
		if (t<=n) f[t]++;
	}
	for (int u=1;u<=n;u++)
	{
		for (int i=u;i<=n;i+=u)
			g[i]=g[i]+f[u];
		f[u]=0;
	}
	int ans=0;
	f[1]=1;
	for (int u=1;u<=n;u++)
	{
	//	printf("YES:%d %d\n",u,g[u]);
		if (siz[1]%u==0&&g[u]==u)
		{
			ans=(ans+f[u])%MOD;
			for (int i=u+u;i<=n;i+=u) f[i]=(f[i]+f[u])%MOD;
		}
	}
	printf("%d\n",ans);
	return 0;
}


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

int N;
ll s[1000005];int fa[1000005];
ll S;
ll f[1000005],ans;
const int p=1000000007;

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

int main(){
	scanf("%d",&N);
	for(int i=1;i<=N;i++) scanf("%d",&s[i]);
	for(int i=2;i<=N;i++) scanf("%d",&fa[i]);
	for(int i=N;i;i--) s[fa[i]]+=s[i];
	S=s[1];
	for(int i=1;i<=N;i++) if(S/gcd(s[i],S)<=N) f[S/gcd(s[i],S)]++;
	for(int i=N;i;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=N;i>1;i--)if(f[i]){
		for(int j=i+i;j<=N;j+=i) f[i]=(f[i]+f[j])%p;
		ans=(ans+f[i])%p;
	}
	printf("%d\n",ans+1);
}


#include <bits/stdc++.h>
#include <stdint.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/rope>
#define int int_fast64_t
#define double long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef pair<int, int> pii;
typedef vector<int> VI;

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, j, k, v) for(int (i) = (j)-((v)<0); (v)>0 ? (i)<(k) : (i)>=(k); (i) += (v))
#define REP(i, j, k) for(int (i) = (j); (i) < (k); ++(i))
#define VIS(x, y) for(__typeof(y.begin()) x = y.begin(); x != y.end(); ++x)
#define MST(x, y) memset((x), (y), sizeof(x))
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define endl '\n'

// #define _DEBUG_ //

#ifdef _DEBUG_
	#define de(...) cout << __VA_ARGS__
    #define ar(x, s, t) { REP(_i_, (s), (t)) cout << (x)[_i_] << ' '; cout << endl; }
	#define IOS() (void)0
#else
	#define de(...) (void)0
    #define ar(...) (void)0
	#define IOS() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); }
#endif

const int maxn = 1e6 + 9;
const int mod = 1e9 + 7;
int n, res, a[maxn], b[maxn], c[maxn], d[maxn], p[maxn];

main() {
	IOS();
	cin >> n;
	REP(i, 0, n) cin >> a[i];
	REP(i, 1, n) cin >> p[i], --p[i];
	FOR(i, n, 1, -1) a[p[i]] += a[i];
	MST(b, 0);
	REP(i, 0, n) if(a[0] / __gcd(a[0], a[i]) <= n) ++b[a[0] / __gcd(a[0], a[i])];
	MST(c, 0);
	REP(i, 1, n + 1) FOR(j, i, n + 1, i) c[j] += b[i];
	res = 0;
	MST(d, 0);
	FOR(i, n + 1, 1, -1) if(c[i] == i) {
		d[i] = 1;
		FOR(j, 2 * i, n + 1, i) d[i] = (d[i] + d[j]) % mod;
	}
//	REP(i, 1, n + 1) if(c[i] == i) {
//		res = (res + d[i]) % mod;
//		FOR(j, 2 * i, n + 1, i) d[j] = (d[j] + d[i]) % mod;
//	}
	cout << d[1] << endl;
}


#include <bits/stdc++.h>
#include <stdint.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/rope>
#define int int_fast64_t
#define double long double

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef pair<int, int> pii;
typedef vector<int> VI;

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, j, k, v) for(int (i) = (j)-((v)<0); (v)>0 ? (i)<(k) : (i)>=(k); (i) += (v))
#define REP(i, j, k) for(int (i) = (j); (i) < (k); ++(i))
#define VIS(x, y) for(__typeof(y.begin()) x = y.begin(); x != y.end(); ++x)
#define MST(x, y) memset((x), (y), sizeof(x))
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define endl '\n'

// #define _DEBUG_ //

#ifdef _DEBUG_
	#define de(...) cout << __VA_ARGS__
    #define ar(x, s, t) { REP(_i_, (s), (t)) cout << (x)[_i_] << ' '; cout << endl; }
	#define IOS() (void)0
#else
	#define de(...) (void)0
    #define ar(...) (void)0
	#define IOS() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); }
#endif

const int maxn = 1e6 + 9;
const int mod = 1e9 + 7;
int n, res, a[maxn], b[maxn], c[maxn], d[maxn], p[maxn];

main() {
	IOS();
	cin >> n;
	REP(i, 0, n) cin >> a[i];
	REP(i, 1, n) cin >> p[i], --p[i];
	FOR(i, n, 1, -1) a[p[i]] += a[i];
	MST(b, 0);
	REP(i, 0, n) if(a[0] / __gcd(a[0], a[i]) <= n) ++b[a[0] / __gcd(a[0], a[i])];
	MST(c, 0);
	REP(i, 1, n + 1) FOR(j, i, n + 1, i) c[j] += b[i];
	d[1] = 1, res = 0;
	REP(i, 1, n + 1) if(c[i] == i) {
		res = (res + d[i]) % mod;
		FOR(j, 2 * i, n + 1, i) d[j] = (d[j] + d[i]) % mod;
	}
	cout << res << endl;
}


#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
    using namespace std;
    #define pii pair<int,int>
    #define fir first
    #define sec second
    #define MP make_pair
    #define rep(i,x,y) for (int i=(x);i<=(y);i++)
    #define drep(i,x,y) for (int i=(x);i>=(y);i--)
    #define go(x) for (int i=head[x];i;i=edge[i].nxt)
    #define templ template<typename T>
    #define sz 1010101
    typedef long long ll;
    typedef double db;
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
    templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
    templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
    templ inline void read(T& t)
    {
        t=0;char f=0,ch=getchar();double d=0.1;
        while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
        while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
        if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
        t=(f?-t:t);
    }
    template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
    char __sr[1<<21],__z[20];int __C=-1,__zz=0;
    inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
    inline void print(register int x)
    {
        if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
        while(__z[++__zz]=x%10+48,x/=10);
        while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
    }
    void file()
    {
        #ifndef ONLINE_JUDGE
        freopen("a.in","r",stdin);
        #endif
    }
    inline void chktime()
    {
        #ifndef ONLINE_JUDGE
        cout<<(clock()-t)/1000.0<<'\n';
        #endif
    }
    #ifdef mod
    ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
    ll inv(ll x){return ksm(x,mod-2);}
    #else
    ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
    #endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

int n;
int fa[sz];
ll sum[sz];
int s[sz];
ll dp[sz];

int main()
{
    file();
    read(n);
    rep(i,1,n) read(sum[i]);
    rep(i,2,n) read(fa[i]);
    drep(i,n,2) sum[fa[i]]+=sum[i];
    rep(i,1,n) for (ll j=sum[1]/__gcd(sum[1],sum[i]),k=j;k<=n;k+=j) ++s[k];
    ll ans=0;
    rep(i,1,n) if (i==s[i])
    {
        if (i!=1) dp[i]++;ans+=dp[i];
        for (int j=i+i;j<=n;j+=i) dp[j]+=dp[i];
    }
    cout<<ans+1;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 1000010
#define ll long long
#define mod 1000000007
using namespace std;
int num[N],fa[N],f[N],cnt[N];
ll sum[N];
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&num[i]);
    for(int i=2;i<=n;i++) scanf("%d",&fa[i]);
    for(int i=n;i;i--) sum[fa[i]]+=(sum[i]+=num[i]);
    for(int i=1;i<=n;i++)
    {
        int s=sum[1]/gcd(sum[i],sum[1]);
        if(s<=n) cnt[s]++;
    }
    for(int i=1;i<=n;i++)
    if(cnt[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;
    ll ans=0;
    for(int i=n;i>1;i--)
    if(f[i])
    {
        for(int j=i*2;j<=n;j+=i) f[i]=(f[i]+f[j])%mod;
        ans=(ans+f[i])%mod;
    }
    printf("%lld\n",ans+1);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 1000010
#define ll long long
#define mod 1000000007
using namespace std;
int num[N],fa[N],f[N],cnt[N];
ll sum[N];
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&num[i]);
    for(int i=2;i<=n;i++) scanf("%d",&fa[i]);
    for(int i=n;i;i--) sum[fa[i]]+=(sum[i]+=num[i]);
    for(int i=1;i<=n;i++)
    {
        ll s=sum[1]/gcd(sum[i],sum[1]);
        if(s<=n) cnt[s]++;
    }
    for(int i=1;i<=n;i++)
    if(cnt[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;
    ll ans=0;
    for(int i=n;i>1;i--)
    if(f[i])
    {
        for(int j=i*2;j<=n;j+=i) f[i]=(f[i]+f[j])%mod;
        ans=(ans+f[i])%mod;
    }
    printf("%lld\n",ans+1);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100010
#define ll long long
#define mod 1000000007
using namespace std;
int num[N],fa[N],f[N],cnt[N];
ll sum[N];
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&num[i]);
    for(int i=2;i<=n;i++) scanf("%d",&fa[i]);
    for(int i=n;i;i--) sum[fa[i]]+=(sum[i]+=num[i]);
    for(int i=1;i<=n;i++)
    {
        int s=sum[1]/gcd(sum[i],sum[1]);
        if(s<=n) cnt[s]++;
    }
    for(int i=1;i<=n;i++)
    if(cnt[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;
    ll ans=0;
    for(int i=n;i>1;i--)
    if(f[i])
    {
        for(int j=i*2;j<=n;j+=i) f[i]=(f[i]+f[j])%mod;
        ans=(ans+f[i])%mod;
    }
    printf("%lld\n",ans+1);
    return 0;
}