#include <bits/stdc++.h>
using namespace std;
template <class T>
void splitstr(const string &s, vector<T> &out) {
  istringstream in(s);
  out.clear();
  copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
static void redirect(int argc, const char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  if (argc > 1) {
    static filebuf f;
    f.open(argv[1], ios::in);
    cin.rdbuf(&f);
    if (!cin) {
      cerr << "Failed to open '" << argv[1] << "'" << endl;
      exit(1);
    }
  }
  if (argc > 2) {
    static filebuf f;
    f.open(argv[2], ios::out | ios::trunc);
    cout.rdbuf(&f);
    if (!cout) {
      cerr << "Failed to open '" << argv[2] << "'" << endl;
    }
  }
}
int main(int argc, const char **argv) {
  redirect(argc, argv);
  int N;
  cin >> N;
  vector<long long> s(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    s[i] = a;
  }
  vector<int> p(N);
  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];
  long long S = s[0];
  vector<int> cands(N + 1);
  for (int i = 0; i < N; i++) {
    long long c = gcd(S, s[i]);
    if (S / c <= N) cands[S / c]++;
  }
  for (int i = N; i >= 1; i--) {
    for (int j = 2 * i; j <= N; j += i) cands[j] += cands[i];
  }
  vector<int> scores(N + 1);
  for (int i = N; i >= 1; i--)
    if (cands[i] == i) {
      long long t = 1;
      for (int j = 2 * i; j <= N; j += i) t += scores[j];
      scores[i] = t % 1000000007;
    }
  cout << scores[1] << '\n';
  return 0;
}
