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