#include <bits/stdc++.h>
using namespace std;
const long long inf = INT_MAX, df = 1e6 + 7, mod = 1e9 + 7;
inline long long read() {
  long long x = 0, y = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') y = (ch == '-') ? -1 : 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * y;
}
long long i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, fa[df], a[df],
    f[df], sum[df], num[df], ans;
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  n = read();
  for (long long i = (1); i <= (n); ++i) a[i] = read();
  for (long long i = (2); i <= (n); ++i) fa[i] = read();
  for (long long i = (n); i >= (1); --i) sum[i] += a[i], sum[fa[i]] += sum[i];
  s = sum[0];
  for (long long i = (1); i <= (n); ++i)
    if (s / gcd(s, sum[i]) <= n) ++num[s / gcd(s, sum[i])];
  for (long long i = (1); i <= (n); ++i) {
    if (num[i]) {
      for (long long j = i; j <= n; j += i) f[j] += num[i];
    }
  }
  for (long long i = (1); i <= (n); ++i) f[i] = (f[i] == i);
  for (long long i = (n); i >= (2); --i) {
    if (!f[i]) continue;
    for (long long j = i * 2; j <= n; j += i) (f[i] += f[j]) %= mod;
    (ans += f[i]) %= mod;
  }
  printf("%lld\n", (ans + 1) % mod);
}
