#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
int p[maxn], cnt[maxn], d[maxn];
long long sum[maxn];
long long gcd(long long x, long long y) {
  if (!y) return x;
  return gcd(y, x % y);
}
void add(int &ans, int tmp) { ans = (ans + tmp) % mod; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%I64d", &sum[i]);
  for (int i = 2; i <= n; i++) scanf("%d", &p[i]);
  for (int i = n; i > 1; i--) sum[p[i]] += sum[i];
  for (int i = 1; i <= n; i++) {
    long long k = sum[1] / gcd(sum[1], sum[i]);
    if (k <= n) cnt[k]++;
  }
  for (int i = n; i; i--)
    for (int j = 2 * i; j <= n; j += i) cnt[j] += cnt[i];
  int ans = 0;
  d[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] != i) continue;
    add(ans, d[i]);
    for (int j = 2 * i; j <= n; j += i) add(d[j], d[i]);
  }
  printf("%d\n", ans);
}
