Problem: There are n cities in the Kingdom of Autumn, numbered from 1 to n. People can travel between any two cities using n-1 two-directional roads.

This year, the government decides to separate the kingdom. There will be regions of different levels. The whole kingdom will be the region of level 1. Each region of i-th level should be separated into several (at least two) regions of i+1-th level, unless i-th level is the last level. Each city should belong to exactly one region of each level and for any two cities in the same region, it should be possible to travel between them passing the cities in the same region only.

According to research, for each city i, there is a value a_i, which describes the importance of this city. All regions of the same level should have an equal sum of city importances.

Your task is to find how many plans there are to determine the separation of the regions that all the conditions are satisfied. Two plans are considered different if and only if their numbers of levels are different or there exist two cities in the same region of one level in one plan but in different regions of this level in the other plan. Since the answer may be very large, output it modulo 10^9+7.

Input

The first line contains one integer n (1 ≤ n ≤ 10^6) — the number of the cities.

The second line contains n integers, the i-th of which is a_i (1 ≤ a_i ≤ 10^9) — the value of each city.

The third line contains n-1 integers, p_1, p_2, …, p_{n-1}; p_i (p_i ≤ i) describes a road between cities p_i and i+1.

Output

Print one integer — the number of different plans modulo 10^9+7.

Examples

Input

4
1 1 1 1
1 2 3


Output

4

Input

4
1 1 1 1
1 2 2


Output

2

Input

4
1 2 1 2
1 1 3


Output

3

Note

For the first example, there are 4 different plans:

Plan 1: Level-1: \{1,2,3,4\}.

Plan 2: Level-1: \{1,2,3,4\}, Level-2: \{1,2\},\{3,4\}.

Plan 3: Level-1: \{1,2,3,4\}, Level-2: \{1\},\{2\},\{3\},\{4\}.

Plan 4: Level-1: \{1,2,3,4\}, Level-2: \{1,2\},\{3,4\}, Level-3: \{1\},\{2\},\{3\},\{4\}.

For the second example, there are 2 different plans:

Plan 1: Level-1: \{1,2,3,4\}.

Plan 2: Level-1: \{1,2,3,4\}, Level-2: \{1\},\{2\},\{3\},\{4\}.

For the third example, there are 3 different plans:

Plan 1: Level-1: \{1,2,3,4\}.

Plan 2: Level-1: \{1,2,3,4\}, Level-2: \{1,2\},\{3,4\}.

Plan 3: Level-1: \{1,2,3,4\}, Level-2: \{1,3\},\{2\},\{4\}.

Solution (in CPP):

Solution:
#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();
  }
}