#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = 1e9 + 7;
long long gcd(long long a, long long b) { return (b) ? (gcd(b, a % b)) : (a); }
int add(int a, int b) { return ((a += b) >= M) ? (a - M) : (a); }
int n, res;
long long ss[N];
int fa[N], f[N], g[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", ss + i);
  for (int i = 2; i <= n; i++) scanf("%d", fa + i);
  for (int i = n; i > 1; i--) ss[fa[i]] += ss[i];
  long long S = ss[1];
  for (int i = 1; i <= n; i++)
    if ((ss[i] = S / gcd(S, ss[i])) <= n) f[ss[i]]++;
  for (int i = n; i; i--)
    for (int j = i << 1; j <= n; j += i) f[j] += f[i];
  g[1] = 1;
  for (int i = 1; i <= n; i++)
    if (f[i] == i) {
      res = add(res, g[i]);
      for (int j = i << 1; j <= n; j += i) g[j] = add(g[j], g[i]);
    }
  printf("%d\n", res);
  return 0;
}
