#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 1e18;
template <typename T>
void read(T &x) {
  x = 0;
  int s = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') s = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
  x *= s;
}
template <typename T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
  write(x);
  puts("");
}
const int maxn = 1e6 + 5, mod = 1e9 + 7;
int n, fa[maxn];
long long a[maxn], dp[maxn], g[maxn], ans;
vector<int> fac[maxn];
long long gcd(long long a, long long 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(fa[i]);
  for (int i = n; i >= 1; i--) a[fa[i]] += a[i];
  for (int i = 1; i <= n; i++) {
    long long k = a[1] / gcd(a[1], a[i]);
    if (k <= n) dp[k]++;
  }
  for (int i = n; i >= 1; i--)
    if (dp[i]) {
      for (int j = i * 2; j <= n; j += i)
        dp[j] += dp[i], fac[j].emplace_back(i);
    }
  ans = g[1] = 1;
  for (int k = 2; k <= n; k++)
    if (dp[k] == k) {
      for (int d : fac[k]) (g[k] += g[d]) %= mod;
      (ans += g[k]) %= mod;
    }
  return !printf("%lld\n", ans);
}
