考虑一个显然的树形 dp,设 \(f_{u, i}\) 为 \(u\) 结点染颜色 \(i\) 的方案数,有 \(f_{u, i} = \prod\limits_{v \in son_u} \sum\limits_{j = 1}^i f_{v, j}\)。前缀和后可得 \(f_{u, i} = f_{u, i - 1} + \prod\limits_{v \in son_u} f_{v, i}\)。
发现 \(f_u(x)\) 为最高次数为 \(sz_u\) 的多项式。考虑归纳证明,叶子结点有 \(f_u(x) = x\)。差分后是儿子的多项式乘积,次数为 \(\sum\limits_{v \in son_u} sz_v = sz_u - 1\)。前缀和后有次数为 \(sz_u\)。
于是树形 dp 算出 \(d = 1, 2, \ldots, n\) 的答案,拉格朗日插值即可。
时间复杂度 \(O(n^2)\)。
code
// Problem: F. Cowmpany Cowmpensation
// Contest: Codeforces - Codeforces Round 492 (Div. 1) [Thanks, uDebug!]
// URL: https://codeforces.com/problemset/problem/995/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 3030;
const ll mod = 1000000007;
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
ll n, m, f[maxn][maxn];
vector<int> G[maxn];
void dfs(int u) {
for (int v : G[u]) {
dfs(v);
}
for (int i = 1; i <= n; ++i) {
f[u][i] = f[u][i - 1];
ll mul = 1;
for (int v : G[u]) {
mul = mul * f[v][i] % mod;
}
f[u][i] = (f[u][i] + mul) % mod;
}
}
void solve() {
scanf("%lld%lld", &n, &m);
for (int i = 2, p; i <= n; ++i) {
scanf("%d", &p);
G[p].pb(i);
}
dfs(1);
ll ans = 0;
for (int i = 0; i <= n; ++i) {
ll x = 1, y = 1;
for (int j = 0; j <= n; ++j) {
if (i == j) {
continue;
}
x = x * (m - j + mod) % mod;
y = y * (i - j + mod) % mod;
}
ans = (ans + f[1][i] * x % mod * qpow(y, mod - 2)) % mod;
}
printf("%lld\n", ans);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}