Problem p03499 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
int to[N], nex[N], beg[N], dep[N], son[N], maxdep[N], id[N], val[N][3], Num[N];
struct node {
  int Zero, One;
};
vector<node> dp[N];
int e, cnt;
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
inline int fpm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = 1ll * res * a % mod;
    a = 1ll * a * a % mod, b /= 2;
  }
  return res;
}
inline int Mod(int Val) {
  if (Val >= mod)
    Val -= mod;
  if (Val < 0)
    Val += mod;
  return Val;
}
inline void add(int x, int y) { to[++e] = y, nex[e] = beg[x], beg[x] = e; }
void dfs(int x) {
  Num[dep[x]]++, maxdep[x] = dep[x];
  for (int i = beg[x]; i; i = nex[i]) {
    dep[to[i]] = dep[x] + 1;
    dfs(to[i]);
    if (maxdep[to[i]] > maxdep[son[x]])
      son[x] = to[i];
    maxdep[x] = max(maxdep[x], maxdep[to[i]]);
  }
}
inline void Dp(int x) {
  int Dep = 0, tot = 0;
  bool flag = 0;
  for (int i = beg[x]; i; i = nex[i])
    Dp(to[i]), Dep = max(Dep, maxdep[to[i]] - dep[x]);
  if (son[x])
    id[x] = id[son[x]];
  Fordown(i, dp[id[x]].size() - 1, dp[id[x]].size() - Dep) {
    val[++tot][0] = dp[id[x]][i].Zero;
    val[tot][1] = dp[id[x]][i].One;
    val[tot][2] = Mod(dp[id[x]][i].Zero + dp[id[x]][i].One);
  }
  for (int i = beg[x]; i; i = nex[i]) {
    flag = 1;
    if (to[i] == son[x])
      continue;
    For(j, 0, dp[id[to[i]]].size() - 1) {
      int pos = dp[id[to[i]]].size() - j;
      val[pos][1] = (1ll * val[pos][0] * dp[id[to[i]]][j].One +
                     1ll * val[pos][1] * dp[id[to[i]]][j].Zero) %
                    mod;
      val[pos][0] = 1ll * val[pos][0] * dp[id[to[i]]][j].Zero % mod;
      val[pos][2] = 1ll * val[pos][2] *
                    (dp[id[to[i]]][j].Zero + dp[id[to[i]]][j].One) % mod;
    }
  }
  For(i, 1, Dep) dp[id[x]][dp[id[x]].size() - i] =
      (node){Mod(val[i][2] - val[i][1] + mod), val[i][1]};
  if (!flag)
    id[x] = ++cnt;
  dp[id[x]].push_back((node){1, 1});
}
int main() {
  int n = read() + 1;
  For(i, 2, n) {
    int x = read() + 1;
    add(x, i);
  }
  dfs(1), Dp(1);
  int ans = 0;
  For(i, 0, dp[id[1]].size() - 1) ans =
      (ans + 1ll * dp[id[1]][i].One * fpm(2, n - Num[maxdep[1] - i])) % mod;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), i##end = (r); i <= i##end; i++)
#define Fordown(i, r, l) for (int i = (r), i##end = (l); i >= i##end; i--)
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
int to[N], nex[N], beg[N], dep[N], son[N], maxdep[N], id[N], val[N][3], Num[N];
struct node {
  int Zero, One;
};
vector<node> dp[N];
int e, cnt;
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
inline int fpm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = 1ll * res * a % mod;
    a = 1ll * a * a % mod, b /= 2;
  }
  return res;
}
inline int Mod(int Val) {
  if (Val >= mod)
    Val -= mod;
  if (Val < 0)
    Val += mod;
  return Val;
}
inline void add(int x, int y) { to[++e] = y, nex[e] = beg[x], beg[x] = e; }
void dfs(int x) {
  Num[dep[x]]++, maxdep[x] = dep[x];
  for (int i = beg[x]; i; i = nex[i]) {
    dep[to[i]] = dep[x] + 1;
    dfs(to[i]);
    if (maxdep[to[i]] > maxdep[son[x]])
      son[x] = to[i];
    maxdep[x] = max(maxdep[x], maxdep[to[i]]);
  }
}
inline void Dp(int x) {
  int Dep = 0, tot = 0;
  bool flag = 0;
  for (int i = beg[x]; i; i = nex[i]) {
    Dp(to[i]);
    if (to[i] == son[x])
      continue;
    Dep = max(Dep, maxdep[to[i]] - dep[x]);
  }
  if (son[x])
    id[x] = id[son[x]];
  Fordown(i, dp[id[x]].size() - 1, dp[id[x]].size() - Dep) {
    val[++tot][0] = dp[id[x]][i].Zero;
    val[tot][1] = dp[id[x]][i].One;
    val[tot][2] = Mod(dp[id[x]][i].Zero + dp[id[x]][i].One);
  }
  for (int i = beg[x]; i; i = nex[i]) {
    flag = 1;
    if (to[i] == son[x])
      continue;
    For(j, 0, dp[id[to[i]]].size() - 1) {
      int pos = dp[id[to[i]]].size() - j;
      val[pos][1] = (1ll * val[pos][0] * dp[id[to[i]]][j].One +
                     1ll * val[pos][1] * dp[id[to[i]]][j].Zero) %
                    mod;
      val[pos][0] = 1ll * val[pos][0] * dp[id[to[i]]][j].Zero % mod;
      val[pos][2] = 1ll * val[pos][2] *
                    (dp[id[to[i]]][j].Zero + dp[id[to[i]]][j].One) % mod;
    }
  }
  For(i, 1, Dep) dp[id[x]][dp[id[x]].size() - i] =
      (node){Mod(val[i][2] - val[i][1] + mod), val[i][1]};
  if (!flag)
    id[x] = ++cnt;
  dp[id[x]].push_back((node){1, 1});
}
int main() {
  int n = read() + 1;
  For(i, 2, n) {
    int x = read() + 1;
    add(x, i);
  }
  dfs(1), Dp(1);
  int ans = 0;
  For(i, 0, dp[id[1]].size() - 1) ans =
      (ans + 1ll * dp[id[1]][i].One * fpm(2, n - Num[maxdep[1] - i])) % mod;
  printf("%d\n", ans);
  return 0;
}