#include <bits/stdc++.h>
using namespace std;
const long long maxd = 1e9 + 7;
long long fa[1001000], n;
long long dp[1001000], w[1001000], sum[1001000];
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}
signed main() {
  n = read();
  long long i, j;
  long long ans = 0;
  memset(dp, 0, sizeof(dp));
  for (i = 1; i <= n; i++) w[i] = read();
  for (i = 2; i <= n; i++) fa[i] = read();
  for (i = n; i >= 1; i--) w[fa[i]] += w[i];
  for (i = n; i >= 1; i--) {
    w[i] = w[1] / gcd(w[1], w[i]);
    if (w[i] <= n) dp[w[i]]++;
  }
  for (i = n; i >= 1; i--)
    for (j = 2 * i; j <= n; j += i) dp[j] = (dp[j] + dp[i]) % maxd;
  sum[1] = 1;
  for (i = 1; i <= n; i++)
    if (dp[i] >= i)
      for (j = i * 2; j <= n; j += i) sum[j] = (sum[j] + sum[i]) % maxd;
  for (i = 1; i <= n; i++)
    if (dp[i] >= i) ans = (ans + sum[i]) % maxd;
  printf("%I64d", ans);
  return 0;
}
