#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *p1, *p2;
template <class T>
inline void read(T &n) {
  char ch =
      (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
           ? 0
           : *p1++);
  T w = 1, x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
             ? 0
             : *p1++);
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch =
        (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)
             ? 0
             : *p1++);
  }
  n = x * w;
}
const int maxn = 1000005;
const int mod = 1e9 + 7;
int n, fa[maxn], f[maxn], ok[maxn];
long long sum[maxn];
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(sum[i]);
  for (int i = 2; i <= n; i++) read(fa[i]);
  for (int i = n; i >= 2; i--) sum[fa[i]] += sum[i];
  for (int i = 1; i <= n; i++) {
    long long tmp = sum[1] / gcd(sum[1], sum[i]);
    if (tmp <= n) f[tmp]++;
  }
  for (int i = n; i >= 1; i--)
    for (int j = i + i; j <= n; j += i) f[j] += f[i];
  for (int i = 1; i <= n; i++) {
    if (sum[1] % i == 0 && f[i] == i) ok[i] = 1;
    f[i] = 0;
  }
  f[1] = 1;
  int res = 0;
  for (int i = 1; i <= n; i++) {
    res += f[i];
    for (int j = i + i; j <= n; j += i)
      if (ok[j]) f[j] += f[i];
  }
  cout << res;
  return 0;
}
