// Solution with O(NK) time and space #include using namespace std; namespace { #define VEVE(i, a, b) for (ll i = a, __##i = b; i < __##i; ++i) #define RARA(i, a, b) for (ll i = a, __##i = b; i > __##i; --i) #define VERA(x, seq) for (auto &x : seq) #define SIZE(x) ((ll)(x.size())) #define ALL(x) x.begin(), x.end() template class IntMod { static_assert(0 < MOD and MOD <= numeric_limits::max() / 2, "MOD out of range"); int val; public: IntMod() {} IntMod(long long x) : val(static_cast(x % MOD)) { val += (val < 0) * MOD; } IntMod& operator+=(const IntMod& x) { val += x.val; val -= (val >= MOD) * MOD; return *this; } IntMod operator+(const IntMod& x) const { IntMod res(*this); return res += x; } IntMod& operator-=(const IntMod& x) { val -= x.val; val += (val < 0) * MOD; return *this; } IntMod operator-(const IntMod& x) const { IntMod res(*this); return res -= x; } IntMod& operator*=(const IntMod& x) { val = static_cast(static_cast(val) * x.val % MOD); return *this; } IntMod operator*(const IntMod& x) const { IntMod res(*this); return res *= x; } IntMod& operator/=(const IntMod& x) { return *this *= x.Inv(); } IntMod operator/(const IntMod& x) const { IntMod res(*this); return res /= x; } IntMod operator-() const { IntMod res(*this); if (res.val != 0) res.val = MOD - res.val; return res; } IntMod Power(int expon) const { IntMod ans(1), temp(*this); for (; expon > 0; expon >>= 1) { ans *= (expon & 1) * temp; temp *= temp; } return ans; } IntMod Inv() const { return Power(MOD - 2); } friend ostream& operator<<(ostream& out, const IntMod& x) { return out << x.val; } bool operator==(const IntMod& x) const { return val == x.val; } bool operator!=(const IntMod& x) const { return val != x.val; } }; typedef int ll; typedef double dd; const ll MOD = ll(1e9) + 7; const ll MAX_N = 200000; const ll MAX_K = 1001; typedef IntMod Int; static Int dp[MAX_N][MAX_K]; vector adj[MAX_N]; vector col, sum; void Dfs(ll at) { VERA(to, adj[at]) { Dfs(to); } static Int temp[MAX_K]; dp[at][0] += 1; sum[at] = 0; VERA(to, adj[at]) { memset(temp, 0, sizeof(ll) * (sum[at] + sum[to] + 1)); VEVE(a, 0, sum[at] + 1) { VEVE(b, 0, sum[to] + 1) { temp[a + b] += dp[at][a] * dp[to][b]; } } sum[at] += sum[to]; memcpy(dp[at], temp, sizeof(ll) * (sum[at] + 1)); } dp[at][col[at]] += 1; sum[at] += col[at]; } void Solve(ll) { ll n, k; cin >> n >> k; VEVE(v, 1, n) { ll p; cin >> p; --p; adj[p].push_back(v); } col.resize(n, 0); VEVE(j, 0, k) { ll v; cin >> v; --v; col[v] = 1; } sum.resize(n, 0); Dfs(0); VEVE(j, 0, k + 1) cout << dp[0][j] << '\n'; } void Init() { ios::sync_with_stdio(false); cin.tie(nullptr); } } int main() { #ifdef AZN freopen("input.txt", "r", stdin); #endif Init(); ll tests = 1; VEVE(test, 1, tests + 1) Solve(test); return 0; }