#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000 + 2;
const long long MOD = 1e9 + 7;
int N, p[MAXN];
long long s[MAXN], c[MAXN], f[MAXN], Ans;
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  return b ? gcd(b, a % b) : a;
}
int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) scanf("%lld", s + i);
  for (int i = 2; i <= N; i++) scanf("%d", p + i);
  for (int i = N; i; i--) s[p[i]] += s[i];
  for (int i = N; i; i--) {
    s[i] = s[1] / gcd(s[1], s[i]);
    if (s[i] <= N) c[s[i]]++;
  }
  for (int i = N; i; i--)
    for (int j = 2 * i; j <= N; j += i) c[j] = (c[j] + c[i]) % MOD;
  f[1] = 1;
  for (int i = 1; i <= N; i++)
    if (c[i] >= i)
      for (int j = 2 * i; j <= N; j += i) f[j] = (f[j] + f[i]) % MOD;
  for (int i = 1; i <= N; i++)
    if (c[i] >= i) Ans = (Ans + f[i]) % MOD;
  cout << Ans << endl;
  return 0;
}
