Problem p03499 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

#define MAX 200002

int n;
vector<int> v[MAX];

deque<tuple<long long int, long long int, long long int>> dp[MAX];

long long int p2[MAX];

#define MOD 1000000007

int sz[MAX];
int dep[MAX];
int mx[MAX];
vector<int> ds[MAX];
inline void dfs2(int b, int pr = -1) {
  mx[b] = 0;
  for (int go : v[b]) {
    dfs2(go, b);
    mx[b] = max(mx[b], mx[go] + 1);
    ds[b].push_back(mx[go] + 1);
  }
}
inline void dfs(int b, int pr = -1, int d = 0, int consider = 0) {
  dep[d]++;
  sz[b] = 1;
  sort(ds[b].begin(), ds[b].end(), greater<int>());
  ds[b].push_back(0);
  for (int go : v[b]) {
    if (go != pr) {
      int nex = consider;
      if (ds[b][0] == mx[go] + 1) {
        if (ds[b].size() >= 1)
          nex = max(nex, ds[b][1]);
      } else {
        nex = max(nex, ds[b][0]);
      }
      dfs(go, b, d + 1, nex - 1);
      sz[b] += sz[go];
      if (dp[b].size() < dp[go].size())
        swap(dp[b], dp[go]);
      for (int k = 0; k < dp[go].size(); k++) {
        long long int a0 = get<0>(dp[b][k]);
        long long int a1 = get<1>(dp[b][k]);
        long long int a2 = get<2>(dp[b][k]);
        long long int b0 = get<0>(dp[go][k]);
        long long int b1 = get<1>(dp[go][k]);
        long long int b2 = get<2>(dp[go][k]);
        long long int c0 = a0 * b0;
        long long int c1 = a1 * b0 + a0 * b1;
        long long int c2 =
            a0 * b2 + a1 * b1 + a1 * b2 + a2 * b0 + a2 * b1 + a2 * b2;
        if (c0 >= MOD)
          c0 %= MOD;
        if (c1 >= MOD)
          c1 %= MOD;
        if (c2 >= MOD)
          c2 %= MOD;
        dp[b][k] = make_tuple(c0, c1, c2);
      }
      dp[go].clear();
    }
  }
  for (int i = 0; i < min((int)dp[b].size(), consider); i++) {
    get<0>(dp[b][i]) += get<2>(dp[b][i]);
    get<0>(dp[b][i]) %= MOD;
    get<2>(dp[b][i]) = 0;
  }
  dp[b].push_front(make_tuple(1, 1, 0));
  return;
}

int main() {
  p2[0] = 1;
  for (int i = 1; i < MAX; i++) {
    p2[i] = p2[i - 1] * 2LL;
    if (p2[i] >= MOD)
      p2[i] %= MOD;
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    v[a].push_back(i);
  }
  dfs2(0);
  dfs(0);
  long long int ans = p2[n];
  for (int i = 1; i < dp[0].size(); i++) {
    long long int tmp = get<1>(dp[0][i]);
    tmp *= p2[n + 1 - dep[i]];
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

#define MAX 200002

int n;
vector<int> v[MAX];

deque<tuple<long long int, long long int, long long int>> dp[MAX];

long long int p2[MAX];

#define MOD 1000000007

int sz[MAX];
int dep[MAX];
int mx[MAX];
vector<int> ds[MAX];
inline void dfs2(int b, int pr = -1) {
  mx[b] = 0;
  for (int go : v[b]) {
    dfs2(go, b);
    mx[b] = max(mx[b], mx[go] + 1);
    ds[b].push_back(mx[go] + 1);
  }
}
inline void dfs(int b, int pr = -1, int d = 0, int consider = 0) {
  dep[d]++;
  sz[b] = 1;
  sort(ds[b].begin(), ds[b].end(), greater<int>());
  ds[b].push_back(0);
  for (int go : v[b]) {
    if (go != pr) {
      int nex = 0;
      if (ds[b][0] == mx[go] + 1) {
        if (ds[b].size() >= 1)
          nex = max(nex, ds[b][1]);
      } else {
        nex = max(nex, ds[b][0]);
      }
      dfs(go, b, d + 1, nex - 1);
      sz[b] += sz[go];
      if (dp[b].size() < dp[go].size())
        swap(dp[b], dp[go]);
      for (int k = 0; k < dp[go].size(); k++) {
        long long int a0 = get<0>(dp[b][k]);
        long long int a1 = get<1>(dp[b][k]);
        long long int a2 = get<2>(dp[b][k]);
        long long int b0 = get<0>(dp[go][k]);
        long long int b1 = get<1>(dp[go][k]);
        long long int b2 = get<2>(dp[go][k]);
        long long int c0 = a0 * b0;
        long long int c1 = a1 * b0 + a0 * b1;
        long long int c2 =
            a0 * b2 + a1 * b1 + a1 * b2 + a2 * b0 + a2 * b1 + a2 * b2;
        if (c0 >= MOD)
          c0 %= MOD;
        if (c1 >= MOD)
          c1 %= MOD;
        if (c2 >= MOD)
          c2 %= MOD;
        dp[b][k] = make_tuple(c0, c1, c2);
      }
      dp[go].clear();
    }
  }
  for (int i = 0; i < min((int)dp[b].size(), consider); i++) {
    get<0>(dp[b][i]) += get<2>(dp[b][i]);
    get<0>(dp[b][i]) %= MOD;
    get<2>(dp[b][i]) = 0;
  }
  dp[b].push_front(make_tuple(1, 1, 0));
  return;
}

int main() {
  p2[0] = 1;
  for (int i = 1; i < MAX; i++) {
    p2[i] = p2[i - 1] * 2LL;
    if (p2[i] >= MOD)
      p2[i] %= MOD;
  }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    v[a].push_back(i);
  }
  dfs2(0);
  dfs(0);
  long long int ans = p2[n];
  for (int i = 1; i < dp[0].size(); i++) {
    long long int tmp = get<1>(dp[0][i]);
    tmp *= p2[n + 1 - dep[i]];
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }
  printf("%lld\n", ans);
  return 0;
}