#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using u128 = __int128_t;
using ull = unsigned long long;
using ld = long double;

#define dbg(x) cerr << #x << " = " << x << endl

const ll LINF = 2e18;
const int INF = 1e9 + 10;
const int LOG = 30;

const int N = 1e6 + 10, K = 2010;
const int MOD = 998244353;
struct modint {
    int val;
    modint(int val_ = 0) : val(val_) {}
    modint(ll val_) : val(val_ % MOD) {}
};
modint operator+(modint a, modint b) {
    int res = a.val + b.val;
    return res >= MOD ? res - MOD : res;
}
modint operator-(modint a, modint b) {
    int res = a.val - b.val;
    return res < 0 ? res + MOD : res;
}
modint operator*(modint a, modint b) {
    return a.val * 1ll * b.val % MOD;
}
void operator+=(modint &a, modint b) {
    int res = a.val + b.val;
    a.val = res >= MOD ? res - MOD : res; 
}
void operator-=(modint &a, modint b) {
    int res = a.val - b.val;
    a.val = res < 0 ? res + MOD : res;
}
void operator*=(modint &a, modint b) {
    a.val = a.val * 1ll * b.val % MOD;
}
modint binpow(modint base, int exp) {
    modint res = 1;
    while (exp) {
        if (exp & 1) res *= base;
        base *= base;
        exp >>= 1;
    }
    return res;
}
modint operator~(modint val) {
    return binpow(val, MOD - 2);
}
modint fact[N], invf[N];
void precalc() {
    fact[0] = 1;
    for (int i = 1; i < N; i++) {
        fact[i] = fact[i - 1] * i;
    }
    invf[N - 1] = ~fact[N - 1];
    for (int i = N - 2; i >= 0; i--) {
        invf[i] = invf[i + 1] * (i + 1);
    }
}
modint C(int k, int n) {
    if (k > n || n < 0) return 0;
    return fact[n] * invf[k] * invf[n - k];
}


vector<int> g[N];
int timer = 0, tin[N], tout[N];
int go[N][LOG];
void dfs(int v) {
    tin[v] = timer++;
    for (int j = 1; j < LOG; j++) {
        go[v][j] = go[go[v][j - 1]][j - 1];
    }
    for (int u : g[v]) {
        dfs(u);
    }
    tout[v] = timer++;
}
bool isp(int v, int u) {
    return tin[v] <= tin[u] && tin[u] < tout[v];
}
int lca(int v, int u) {
    if (isp(v, u)) return v;
    if (isp(u, v)) return u;
    for (int j = LOG - 1; j >= 0; j--) {
        if (!isp(go[v][j], u)) {
            v = go[v][j];
        }
    }
    return go[v][0];
}
int lca(vector<int> a) {
    int l = a[0];
    for (int i = 1; i < a.size(); i++) {
        l = lca(l, a[i]);
    }
    return l;
}

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> need(k);
    for (int i = 0; i < k; i++) {
        cin >> need[i];
        need[i]--;
    }
    for (int i = 1; i < n; i++) {
        int p;
        cin >> p;
        p--;
        go[i][0] = p;
        g[p].push_back(i);
    }
    dfs(0);
    // cout << lca(1, 2) << endl;
    // cout << lca({1, 2}) << endl;

    int ans = 0;
    vector<vector<int>> group(k);
    auto rec = [&](auto&& self, int i) -> void {
        // cout << "THERE" << endl;
        if (i == n) {
            // cout << "HERE" << endl;
            for (int j = 0; j < k; j++) {
                if (group[j].size() == 0) return;
                // for (int x : group[j]) cout << x << " "; cout << endl;
                if (lca(group[j]) != need[j]) return;
            }
            ans++;
            return;
        }
        self(self, i + 1);
        for (int j = 0; j < k; j++) {
            group[j].push_back(i);
            self(self, i + 1);
            group[j].pop_back();
        }
    };
    rec(rec, 0);
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    precalc();
    while (t--) {
        solve();
    }
    return 0;
}
