#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long

using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,unroll-loops,O3")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int N = 1e6;
const int MOD = 998244353;

int n, k;

int a[N];
vector<int> g[N];
int ans = 0;

void calc(vector<char>& can, int v) {
    can[v] = 1;
    for (int& u : g[v]) {
        calc(can, u);
    }
}

int ancestor(vector<vector<char>>& can, int v, int u) {
    for (int i = n - 1; i >= 0; --i) {
        if (can[v][i] && can[u][i]) {
            return i;
        }
    }

    return -1;
}

int get(vector<vector<char>>& can, vector<int>& a) {
    int n = a.size();

    int res = a[0];

    for (int i = 1; i < n; ++i) {
        res = ancestor(can, res, a[i]);
        if (res == -1) {
            return -1;
        }
    }

    return res;
}

void dfs(vector<vector<char>>& can, vector<int>& cur, int idx, int pref) {
    if (idx == k) {
        vector<int> b;
        for (int i = 0; i < k; ++i) {
            for (int j = 0; j < cur[i]; ++j) {
                b.push_back(i);
            }
        }
        do {
            bool flag = true;
            for (int i = 0; i < k; ++i) {
                vector<int> tmp;
                for (int j = 0; j < n; ++j) {
                    if (b[j] == i) {
                        tmp.push_back(j);
                    }
                }
                int res = get(can, tmp);
                if (res != a[i]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                ++ans;
            }
        } while (next_permutation(b.begin(), b.end()));
        return;
    }

    int last = k - idx - 1;

    for (int i = 1; i <= k - pref - last; ++i) {
        cur.push_back(i);
        dfs(can, cur, idx + 1, pref + i);
        cur.pop_back();
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

//    random_device rnd;
//    mt19937 gen(rnd());
//    uniform_int_distribution<> dis(0, n - 1);
//    uniform_real_distribution<> dis1(0, 1);

    cin >> n >> k;

    for (int i = 0; i < k; ++i) {
        cin >> a[i];
    }

    for (int i = 1; i < n; ++i) {
        int p;
        cin >> p;
        --p;
        g[i].push_back(p);
    }

    vector<vector<char>> can(n, vector<char>(n, 0));

    for (int i = 0; i < n; ++i) {
        calc(can[i], i);
    }

    vector<int> cur(k);

    dfs(can, cur, 0, 0);

    cout << ans;
}
