#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010, inf = 1e9, mod = 1e9 + 7;
int n, ans;
int fa[maxn], cnt[maxn], f[maxn];
long long a[maxn];
template <typename T>
inline void read(T &k) {
  int f = 1;
  k = 0;
  char c = getchar();
  while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar();
  while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar();
  k *= f;
}
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; i--) a[fa[i]] += a[i];
  for (int i = 1; i <= n; i++) {
    long long tmp = a[1] / gcd(a[1], a[i]);
    if (tmp <= n) cnt[tmp]++;
  }
  for (int i = n; i; i--) {
    for (int j = i + i; j <= n; j += i) cnt[j] += cnt[i];
  }
  f[1] = 1;
  for (int i = 1; i <= n; i++)
    if (a[1] % i == 0 && cnt[i] == i) {
      ans = ((ans + f[i]) >= mod ? (ans + f[i]) - mod : (ans + f[i]));
      for (int j = i + i; j <= n; j += i)
        if (a[1] % j == 0 && cnt[j] == j)
          f[j] = ((f[j] + f[i]) >= mod ? (f[j] + f[i]) - mod : (f[j] + f[i]));
    }
  printf("%d\n", ans);
}
