#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long s[N];
int f[N], p[N], ans[N];
int main() {
  int n, Ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &s[i]);
  for (int i = 2; i <= n; i++) scanf("%d", &p[i]);
  for (int i = n; i >= 1; i--) s[p[i]] += s[i];
  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--)
    for (int j = 2 * i; j <= n; j += i) (f[j] += f[i]) %= mod;
  ans[1] = 1;
  for (int i = 1; i <= n; i++)
    if (f[i] >= i)
      for (int j = 2 * i; j <= n; j += i) (ans[j] += ans[i]) %= mod;
  for (int i = 1; i <= n; i++)
    if (f[i] >= i) (Ans += ans[i]) %= mod;
  printf("%d\n", Ans);
  return 0;
}
