#include <bits/stdc++.h>
const long long md = 1e9 + 7;
const int Inf = 1e9;
const long long Inf64 = 1e18;
const long long MaxN = 511;
const long long MaxM = 11;
const long double eps = 1e-15;
const long long dx[4] = {0, 1, 0, -1};
const long long dy[4] = {1, 0, -1, 0};
const long long ddx[4] = {1, 1, -1, -1};
const long long ddy[4] = {1, -1, 1, -1};
using namespace std;
long long gcd(long long a, long long b) {
  while (a) {
    b %= a;
    swap(a, b);
  }
  return b;
}
long long gcdex(long long a, long long mod = md) {
  long long g = mod, r = a, x = 0, y = 1;
  while (r != 0) {
    long long q = g / r;
    g %= r;
    swap(g, r);
    x -= q * y;
    swap(x, y);
  }
  return x < 0 ? x + mod : x;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);
  long long N;
  cin >> N;
  vector<long long> Mt(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> Mt[i + 1];
  }
  vector<long long> Pt(N + 1);
  for (int i = 1; i < N; i++) {
    cin >> Pt[i + 1];
  }
  for (int i = N; i > 1; i--) {
    Mt[Pt[i]] += Mt[i];
  }
  vector<long long> Cnt(N + 1);
  for (int i = 1; i <= N; i++) {
    long long K = Mt[1] / gcd(Mt[i], Mt[1]);
    if (K <= N) {
      Cnt[K]++;
    }
  }
  for (int i = N - 1; i > 0; i--) {
    for (int j = i + i; j <= N; j += i) Cnt[j] += Cnt[i];
  }
  vector<long long> Rt(N + 1);
  long long Ans = 0;
  Rt[1] = 1;
  for (int i = 1; i <= N; i++) {
    if (Cnt[i] != i) continue;
    Rt[i] %= md;
    Ans += Rt[i];
    for (int j = i + i; j <= N; j += i) Rt[j] += Rt[i];
  }
  cout << Ans;
  cerr << '\n'
       << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
       << '\n';
  return 0;
}
