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