#include <bits/stdc++.h>
using namespace std;
inline int inn() {
  int x, ch;
  while ((ch = getchar()) < '0' || ch > '9')
    ;
  x = ch ^ '0';
  while ((ch = getchar()) >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x;
}
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long s[1000010];
int f[1000010], p[1000010], ans[1000010];
int main() {
  int n = inn(), Ans = 0;
  for (int i = 1; i <= n; i++) s[i] = inn();
  for (int i = 2; i <= n; i++) p[i] = inn();
  for (int i = n; i; i--) s[p[i]] += s[i];
  for (int i = n; i; 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; i--)
    for (int j = 2 * i; j <= n; j += i)
      (f[j] += f[i], (f[j] >= 1000000007 ? f[j] -= 1000000007 : 0));
  for (int i = ans[1] = 1; i <= n; i++)
    if (f[i] >= i)
      for (int j = i * 2; j <= n; j += i)
        (ans[j] += ans[i], (ans[j] >= 1000000007 ? ans[j] -= 1000000007 : 0));
  for (int i = 1; i <= n; i++)
    if (f[i] >= i) (Ans += ans[i], (Ans >= 1000000007 ? Ans -= 1000000007 : 0));
  return !printf("%d\n", Ans);
}
