#include <bits/stdc++.h>
using namespace std;
inline void open(const char *s) {}
inline int rd() {
  static int x, f;
  x = 0;
  f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return f > 0 ? x : -x;
}
const int N = 1000010, mod = 1000000007;
int n, fa[N], f[N], g[N];
long long s[N];
inline int upd(int &a, int b) {
  a += b;
  return a >= mod ? a -= mod : a;
}
inline long long gcd(long long a, long long b) {
  if (!a || !b) return a ^ b;
  while (a ^= b ^= a ^= b %= a)
    ;
  return b;
}
int main() {
  n = rd();
  for (int i = 1; i <= n; i++) s[i] = rd();
  for (int i = 2; i <= n; i++) fa[i] = rd();
  for (int i = n; i >= 2; i--) s[fa[i]] += s[i];
  long long S = s[1];
  for (int i = n; i >= 1; i--) s[i] = s[1] / gcd(s[1], s[i]);
  for (int i = 1; i <= n; i++)
    if (s[i] <= n) ++f[s[i]];
  for (int i = n; i >= 1; i--)
    if (f[i])
      for (int j = i + i; j <= n; j += i) upd(f[j], f[i]);
  for (int i = 1; i <= n; i++)
    if (f[i] == i && S % i == 0) g[i] = 1;
  memset(f, 0, N << 2);
  f[1] = g[1];
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (g[i]) {
      upd(ans, f[i]);
      for (int j = i + i; j <= n; j += i)
        if (g[j]) upd(f[j], f[i]);
    }
  printf("%d\n", ans);
  return 0;
}
