#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
long long ksm(long long a, long long b) {
  long long r = 1;
  while (b) {
    if (b & 1) r = (r * a) % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return r;
}
void exgcd(long long a, long long b, long long &x, long long &y, long long &d) {
  if (!b)
    d = a, x = 1, y = 0;
  else
    exgcd(b, a % b, y, x, d), y -= a / b * x;
}
long long inv(long long a, long long p) {
  long long x, y, d;
  exgcd(a, p, x, y, d);
  x = (x % p + p) % p;
  return x;
}
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
const int maxn = 1e6 + 100;
int n, m, k, t;
long long dp[maxn], val[maxn];
vector<int> g[maxn];
long long cnt[maxn];
long long dfs(int u, int fa) {
  for (int v : g[u]) {
    if (v == fa) continue;
    dp[u] += dfs(v, u);
  }
  dp[u] += val[u];
  return dp[u];
}
long long ans[maxn];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) val[i] = read();
  for (int i = 1; i <= n - 1; i++) {
    int u = read(), v = i + 1;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    long long tmp = dp[1] / gcd(dp[1], dp[i]);
    if (tmp <= n) cnt[tmp]++;
  }
  for (int i = n; i >= 1; i--) {
    for (int j = 2 * i; j <= n; j += i) {
      cnt[j] += cnt[i];
      cnt[j] %= mod;
    }
  }
  ans[1] = 1;
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 2 * i; j <= n; j += i) {
      if (cnt[i] >= i) {
        ans[j] += ans[i];
        ans[j] %= mod;
      }
    }
    if (cnt[i] >= i) res += ans[i], res %= mod;
  }
  cout << res << endl;
  return 0;
}
