#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  return (x % y == 0) ? y : gcd(y, x % y);
}
int cnt[1000005], size[1000005];
int f[1000005];
int p[1000005];
long long sumv[1000005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &sumv[i]);
  for (int i = 2; i <= n; i++) scanf("%d", &p[i]);
  for (int i = n; i > 1; i--) sumv[p[i]] += sumv[i];
  long long s = sumv[1];
  for (int i = 1; i <= n; i++) {
    long long d = gcd(s, sumv[i]);
    if (s / d <= n) cnt[s / d]++;
  }
  for (int i = 1; i <= n; i++)
    if (cnt[i]) {
      long long d = s / i;
      for (int j = 1; i * j <= n; j++)
        if (d % j == 0) size[i * j] += cnt[i];
    }
  for (int i = n; i > 0; i--)
    if (size[i] == i) {
      f[i] = 1;
      for (int j = 2; i * j <= n; j++) f[i] = (f[i] + f[i * j]) % 1000000007;
    }
  printf("%d\n", f[1]);
  return 0;
}
