#include <bits/stdc++.h>
const int z = 1e9 + 7;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  int n;
  scanf("%d", &n);
  int a[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int p[n];
  for (int i = 1; i < n; i++) {
    scanf("%d", &p[i]);
    p[i]--;
  }
  long long s[n];
  for (int i = 0; i < n; i++) {
    s[i] = a[i];
  }
  for (int i = n - 1; i >= 1; i--) {
    s[p[i]] += s[i];
  }
  int k[n + 1];
  memset(k, 0, sizeof(k));
  for (int i = 0; i < n; i++) {
    long long t = s[0] / gcd(s[0], s[i]);
    if (t <= n) {
      k[t]++;
    }
  }
  for (int i = n; i >= 1; i--) {
    for (int j = i * 2; j <= n; j += i) {
      k[j] += k[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    k[i] = k[i] == i;
  }
  for (int i = n; i >= 1; i--) {
    if (k[i]) {
      for (int j = i * 2; j <= n; j += i) {
        k[i] = (k[i] + k[j]) % z;
      }
    }
  }
  printf("%d\n", k[1]);
  return 0;
}
