#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int p[(int)1e6];
long long s[(int)1e6];
long long dp[(int)1e6 + 1];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> s[i];
  for (int i = 1; i < n; ++i) {
    cin >> p[i];
    --p[i];
  }
  for (int i = n - 1; i > 0; --i) s[p[i]] += s[i];
  for (int i = 0; i < n; ++i) {
    const long long j = s[0] / gcd(s[0], s[i]);
    if (j <= n) dp[j]++;
  }
  for (int i = n; i >= 1; --i)
    for (int j = i + i; j <= n; j += i) dp[j] += dp[i];
  for (int i = n; i >= 1; --i)
    if (dp[i] = dp[i] >= i && s[0] % i == 0) {
      for (int j = i + i; j <= n; j += i) dp[i] += dp[j];
    }
  cout << dp[1] << '\n';
  return 0;
}
