#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
int cnt[N], dp[N], f[N], fa[N];
long long s[N];
int n, ans;
long long gcd(long long n, long long m) {
  return n % m == 0 ? m : gcd(m, n % m);
}
int main() {
  scanf("%d", &n);
  dp[1] = 1;
  for (int i = 1; i <= n; i++) scanf("%lld", &s[i]);
  for (int i = 2; i <= n; i++) scanf("%d", &fa[i]);
  for (int i = n; i >= 2; i--) s[fa[i]] += s[i];
  for (int i = 1; i <= n; i++) {
    long long t = s[1] / gcd(s[1], s[i]);
    if (t <= n) cnt[t]++;
  }
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j += i) f[j] += cnt[i];
  for (int i = 1; i <= n; i++) {
    ans = (ans + dp[i]) % mod;
    for (int j = i * 2; j <= n; j += i)
      if (s[1] % j == 0 && f[j] == j) dp[j] = (dp[j] + dp[i]) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
