#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXV = 1e6 + 10;
long long gcd(long long X, long long Y) { return Y > 0 ? gcd(Y, X % Y) : X; }
vector<long long> adj[MAXV];
long long S[MAXV];
void dfs(long long X) {
  for (long long i = 0; i < adj[X].size(); i++) {
    dfs(adj[X][i]);
    S[X] += S[adj[X][i]];
  }
  return;
}
long long N, P, sum, t;
long long i, j;
long long cnt[MAXV], dp[MAXV];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  for (i = 0; i < N; i++) cin >> S[i];
  for (i = 1; i < N; i++) {
    cin >> P;
    adj[P - 1].push_back(i);
  }
  dfs(0);
  for (i = 0; i < N; i++) {
    t = S[0] / gcd(S[0], S[i]);
    if (t <= N) cnt[t]++;
  }
  for (i = N; i > 0; i--)
    for (j = i << 1; j <= N; j += i) cnt[j] += cnt[i];
  dp[1] = 1;
  for (i = 1; i <= N; i++) {
    if (cnt[i] >= i) {
      sum += dp[i];
      for (j = i << 1; j <= N; j += i) dp[j] += dp[i];
    }
  }
  cout << sum % MOD << endl;
  return 0;
}
