#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool umax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
template <class T>
T min(T a, T b, T c) {
  return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
const long long linf = 2e17;
const int inf = 1e9;
void Fan_chipu();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  Fan_chipu();
}
const int maxn = 1e6 + 7;
long long mod = 1e9 + 7;
int par[maxn], cnt[maxn], dp[maxn];
long long a[maxn];
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
void Fan_chipu() {
  int n;
  cin >> n;
  for (int i = 1; i <= (int)(n); ++i) cin >> a[i];
  for (int i = (int)(2); i <= (int)(n); ++i) cin >> par[i];
  for (int i = n; i >= 2; --i) a[par[i]] += a[i];
  for (int i = 1; i <= (int)(n); ++i) {
    long long tmp = a[1] / gcd(a[1], a[i]);
    if (tmp <= n) ++cnt[tmp];
  }
  for (int i = n; i; --i) {
    for (int j = i + i; j <= n; j += i) cnt[j] += cnt[i];
  }
  long long ans = 0;
  dp[1] = 1;
  for (int i = 1; i <= (int)(n); ++i) {
    if (cnt[i] != i) continue;
    ans += dp[i];
    ans %= mod;
    for (int j = i + i; j <= n; j += i) dp[j] += dp[i], dp[j] %= mod;
  }
  cout << ans;
}
