#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  while (a) swap(a, b %= a);
  return b;
}
long long suma[1000010];
vector<int> adia[1000010];
void dfs(int nod, int tata) {
  for (auto i : adia[nod]) {
    if (i != tata) {
      dfs(i, nod);
      suma[nod] += suma[i];
    }
  }
}
int frc[1000010];
long long nrmods[1000010];
int main() {
  long long s = 0;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", suma + i);
    s += suma[i];
  }
  for (int i = 2; i <= n; i++) {
    int x;
    scanf("%d", &x);
    adia[x].push_back(i);
    adia[i].push_back(x);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    long long k = s / gcd(s, suma[i]);
    if (k <= n) frc[k]++;
  }
  for (int i = n; i >= 1; i--)
    if (frc[i])
      for (int j = 2 * i; j <= n; j += i) frc[j] += frc[i];
  int ans = 0;
  nrmods[1] = 1;
  for (int i = 1; i <= n; i++) {
    assert(frc[i] <= i);
    if (frc[i] == i) {
      nrmods[i] %= 1000000007;
      ans = (ans + nrmods[i]) % 1000000007;
      for (int j = 2 * i; j <= n; j += i) nrmods[j] += nrmods[i];
    }
  }
  cout << ans << '\n';
  return 0;
}
