#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000050;
int a[maxn], p[maxn];
long long f[maxn], sz[maxn];
long long cnt[maxn];
vector<int> fac[maxn];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), sz[i] = a[i];
  for (int i = 2; i <= n; ++i) scanf("%d", p + i);
  for (int i = n; i >= 2; --i) sz[p[i]] += sz[i];
  for (int i = 1; i <= n; ++i) {
    long long k = sz[1] / gcd(sz[i], sz[1]);
    if (k <= n) cnt[k]++;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + i; j <= n; j += i) {
      fac[j].push_back(i);
    }
  }
  f[1] = 1;
  const int MOD = 1e9 + 7;
  long long ans = 1;
  for (int i = 2; i <= n; ++i) {
    int sum = cnt[i];
    for (auto e : fac[i]) sum += cnt[e];
    if (sum < i)
      f[i] = 0;
    else {
      for (auto e : fac[i]) {
        f[i] = (f[i] + f[e]) % MOD;
      }
    }
    ans = (ans + f[i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}
