#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
const int maxn = 1e6 + 10;
long long sum[maxn], f[maxn], g[maxn];
int p[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &sum[i]);
  for (int i = 2; i <= n; i++) scanf("%d", &p[i]);
  for (int i = n; i >= 2; i--) sum[p[i]] += sum[i];
  for (int i = 1; i <= n; i++) {
    long long d = sum[1] / gcd(sum[1], sum[i]);
    if (d <= n) f[d]++;
  }
  for (int i = n; i >= 1; i--) {
    for (int j = i * 2; j <= n; j += i) f[j] += f[i];
  }
  for (int i = n; i >= 1; i--) {
    if (f[i] >= i) {
      g[i]++;
      for (int j = i * 2; j <= n; j += i) g[i] += g[j];
    }
  }
  printf("%lld\n", g[1]);
  return 0;
}
