#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const long long LL_INF = 1ll * INF * INF;
const int MAX_N = 1000000 + 7;
template <typename T>
inline void addmod(T& a, const long long& b, const int& MOD = INF) {
  a = (a + b) % MOD;
  if (a < 0) a += MOD;
}
const int mod = INF;
long long f[MAX_N];
int n;
vector<int> adj[MAX_N];
vector<int> rev[MAX_N];
long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); }
vector<int> divisors[MAX_N];
void init() {
  for (int i = 1; i < MAX_N; ++i)
    if (!rev[i].empty()) {
      for (int j = i; j < MAX_N; j += i) divisors[j].push_back(i);
    }
}
class FenWickTree {
 public:
  void update(int x, int val) {
    for (int t = x; t < MAX_N; t += t & -t) {
      tree[t] += val;
    }
  }
  int get(int x) {
    int res = 0;
    for (int t = x; t > 0; t -= t & -t) {
      res += tree[t];
    }
    return res;
  }

 private:
  static const int MAX_N = ::MAX_N + 7;
  int tree[MAX_N];
} fenwickTree;
int level[MAX_N];
int low[MAX_N], high[MAX_N];
void dfs(int u) {
  static int cnt = 0;
  low[u] = ++cnt;
  for (int v : adj[u]) {
    level[v] = level[u] + 1;
    dfs(v);
    f[u] += f[v];
  }
  high[u] = cnt;
}
bool check(int d) {
  if (d == 1) return true;
  if (f[0] % d) return false;
  long long sum = f[0] / d;
  int cnt = 0;
  for (int c : divisors[d])
    if (!rev[c].empty()) {
      cnt += rev[c].size();
    }
  return d == cnt;
}
int ans[MAX_N];
void solve() {
  cin >> n;
  for (int i = (0), _b = (n); i < _b; ++i) cin >> f[i];
  for (int i = (1), _b = (n); i < _b; ++i) {
    int p;
    cin >> p;
    adj[p - 1].push_back(i);
  }
  dfs(0);
  for (int i = 0; i < n; ++i) {
    long long d = gcd(f[i], f[0]);
    if (f[0] / d < MAX_N) {
      rev[f[0] / d].push_back(i);
    }
  }
  init();
  for (int i = n; i > 0; --i)
    if (check(i)) {
      ans[i] += 1;
      if (ans[i] >= mod) ans[i] -= mod;
      for (int c : divisors[i])
        if (c != i) {
          ans[c] += ans[i];
          if (ans[c] >= mod) ans[c] -= mod;
        }
    }
  cout << ans[1] << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  const bool multiple_test = false;
  int test = 1;
  if (multiple_test) cin >> test;
  for (int i = 0; i < test; ++i) {
    solve();
  }
}
