#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int sum(int a, int b) { return (a + b) % MOD; }
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int N = 1e6 + 5;
long long s[N];
int par[N];
int cnt[N], dp[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%lld", s + i);
  for (int i = 1; i < n; ++i) {
    scanf("%d", par + i);
    --par[i];
  }
  for (int i = n - 1; i > 0; --i) s[par[i]] += s[i];
  long long S = s[0];
  for (int i = 0; i < n; ++i) {
    static long long d;
    d = S / gcd(S, s[i]);
    if (d <= n) ++cnt[d];
  }
  for (int x = n; x > 0; --x)
    for (int y = x + x; y <= n; y += x) cnt[y] += cnt[x];
  for (int d = 1; d <= n; ++d) dp[d] = (cnt[d] == d);
  for (int x = n; x >= 1; --x) {
    if (dp[x] == 0) continue;
    for (int d = x + x; d <= n; d += x) dp[x] = sum(dp[x], dp[d]);
  }
  printf("%d", dp[1]);
}
