#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
vector<int> v[N];
long long sz[N], a[N], f[N], ans;
int n, num[N];
long long gcd(long long A, long long B) { return B == 0 ? A : gcd(B, A % B); }
void dfs(int x) {
  sz[x] = a[x];
  int l = v[x].size(), y;
  for (register int i = (0); i <= (l - 1); ++i) {
    y = v[x][i];
    dfs(y);
    sz[x] += sz[y];
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int x;
  cin >> n;
  for (register int i = (1); i <= (n); ++i) cin >> a[i];
  for (register int i = (2); i <= (n); ++i) cin >> x, v[x].push_back(i);
  dfs(1);
  long long s = sz[1];
  for (register int i = (1); i <= (n); ++i)
    if (s / gcd(s, sz[i]) <= n) num[s / gcd(s, sz[i])]++;
  for (register int i = (n); i >= (1); --i)
    for (int j = 2 * i; j <= n; j += i) num[j] += num[i];
  f[1] = 1;
  for (register int i = (1); i <= (n); ++i) {
    f[i] = f[i] * (num[i] == i);
    for (int j = 2 * i; j <= n; j += i) f[j] += f[i];
    ans += f[i];
  }
  cout << ans << '\n';
  return 0;
}
