#include <bits/stdc++.h>
using namespace std;
long long inline read() {
  long long num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}
const long long maxn = 1000010, mod = 1e9 + 7;
long long n, a[maxn], fa[maxn], s, f[maxn], g[maxn], ans;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) a[i] = read(), s += a[i];
  for (long long i = 2; i <= n; i++) fa[i] = read();
  for (long long i = n; i >= 1; i--) a[fa[i]] += a[i];
  for (long long i = 1; i <= n; i++)
    if (s / gcd(s, a[i]) <= n) f[s / gcd(s, a[i])]++;
  for (long long i = n; i >= 1; i--)
    if (s % i == 0) {
      for (long long j = 2 * i; j <= n; j += i)
        if (s % j == 0) f[j] = (f[i] + f[j]) % mod;
    }
  for (long long i = 1; i <= n; i++)
    if (s % i == 0) f[i] = (f[i] == i);
  g[1] = 1;
  for (long long i = 1; i <= n; i++)
    if (s % i == 0) {
      for (long long j = 2 * i; j <= n; j += i)
        g[j] = (g[j] + f[j] * g[i]) % mod;
      ans = (ans + g[i]) % mod;
    }
  cout << ans;
  return 0;
}
