#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const long long P = 1e9 + 7;
int n, fa[N];
long long a[N], sum[N], f[N], cnt[N];
template <typename T>
inline void read(T &X) {
  X = 0;
  char ch = 0;
  T op = 1;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-') op = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    X = (X << 3) + (X << 1) + ch - 48;
  X *= op;
}
inline long long gcd(long long x, long long y) {
  return (!y) ? x : gcd(y, x % y);
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    sum[i] = a[i];
  }
  for (int i = 2; i <= n; i++) read(fa[i]);
  for (int i = n; i >= 2; i--) sum[fa[i]] += sum[i];
  for (int i = 1; i <= n; i++) {
    long long t = 1LL * sum[1] / gcd(sum[1], sum[i]);
    if (t <= n) cnt[t]++;
  }
  for (int i = n; i >= 1; i--)
    for (int j = i * 2; j <= n; j += i) cnt[j] += cnt[i];
  for (int i = n; i >= 1; i--) {
    if (cnt[i] >= i) {
      f[i] = 1;
      for (int j = 2 * i; j <= n; j += i) (f[i] += f[j]) %= P;
    }
  }
  printf("%I64d\n", f[1]);
  return 0;
}
