#include <bits/stdc++.h>
using namespace std;
int n, nr, cnt[1000000 + 10];
long long val[1000000 + 10], v[1000000 + 10], ans[1000000 + 10];
vector<int> nod[1000000 + 10];
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
void dfs(int x, int p) {
  for (auto u : nod[x]) {
    dfs(u, x);
    val[x] += val[u];
  }
  val[x] += v[x];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> v[i];
  for (int i = 2; i <= n; i++) {
    int x;
    cin >> x;
    nod[x].push_back(i);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    long long k = val[1] / gcd(val[1], val[i]);
    for (long long j = k; j <= n; j += k) cnt[j]++;
  }
  for (int i = n; i; i--) {
    if (val[1] % i != 0 || cnt[i] != i) continue;
    ans[i] = 1;
    for (int j = 2 * i; j <= n; j += i) ans[i] = (ans[i] + ans[j]) % 1000000007;
  }
  cout << ans[1] << '\n';
  return 0;
}
