#include <bits/stdc++.h>
using namespace std;
const int Mod = 1000000007;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long s[1000010];
int f[1000010];
int cnt[1000010];
int p[1000010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &s[i]);
  for (int i = 2; i <= n; i++) scanf("%d", &p[i]);
  for (int i = n; i >= 1; i--) s[p[i]] += s[i];
  long long S = s[1];
  for (int i = 1; i <= n; i++) {
    long long x = S / gcd(S, s[i]);
    if (x <= n) cnt[x]++;
  }
  for (int i = n; i >= 1; i--)
    for (int j = i + i; j <= n; j += i) cnt[j] += cnt[i];
  f[1] = 1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (cnt[i] < i) continue;
    ans = (ans + f[i]) % Mod;
    for (int j = i + i; j <= n; j += i) f[j] = (f[j] + f[i]) % Mod;
  }
  printf("%d\n", ans);
  return 0;
}
