#include <bits/stdc++.h>
using namespace std;
const int nmax = 1000 * 1000 + 5;
const int mod = 1000 * 1000 * 1000 + 7;
long long val[nmax];
int valid[nmax], tt[nmax], ans[nmax];
long long sum, x, C;
long long cate[nmax];
int n, i, j, tot;
long long gcd(long long A, long long B) {
  while (B) {
    C = A % B;
    A = B;
    B = C;
  }
  return A;
}
int md(int x) {
  if (x >= mod) x -= mod;
  return x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> val[i];
    sum += val[i];
  }
  for (i = 2; i <= n; i++) {
    cin >> tt[i];
  }
  for (i = n; i >= 1; i--) {
    val[tt[i]] += val[i];
    x = gcd(sum, val[i]);
    if (sum / x <= n) {
      cate[sum / x]++;
    }
  }
  for (i = n; i >= 1; i--)
    for (j = 2 * i; j <= n; j += i) cate[j] += cate[i];
  for (long long i = 1; i <= n; i++)
    if (sum % i == 0) {
      if (cate[i] == i) valid[i] = 1;
    }
  for (i = 1; i <= n; i++)
    if (valid[i]) ans[i] = 1;
  for (i = n; i >= 1; i--)
    if (valid[i])
      for (j = 2 * i; j <= n; j += i) ans[i] = md(ans[i] + ans[j]);
  for (i = 2; i <= n; i++)
    if (valid[i]) tot = md(tot + ans[i]);
  cout << (tot + 1) % mod;
  return 0;
}
