Problem p03499 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mo = 1000000007;
int hm, n, x, y, t, fa[200005], T[200005], e[200005], h[200005];
LL X, ans, P[200005], Q[200005], A[200005], f[200005], g[200005], H[200005];
vector<int> a[200005], b[200005];
struct cmp {
  bool operator()(int a, int b) {
    return h[a] == h[b] ? e[a] < e[b] : h[a] < h[b];
  }
};
priority_queue<int, vector<int>, cmp> d;
void dfs(int x, int y) {
  fa[x] = y;
  h[x] = h[y] + 1;
  hm = max(hm, h[x]);
  e[x] = ++t;
  if (!T[x])
    T[x] = x;
  b[h[x]].push_back(x);
  if (a[x].size() == 1)
    T[a[x][0]] = T[x];
  for (int i = 0; i < a[x].size(); ++i)
    dfs(a[x][i], x);
}
int main() {
  scanf("%d", &n);
  ++n;
  H[0] = 1;
  for (int i = 1; i <= n; ++i)
    H[i] = H[i - 1] * 2 % mo;
  for (int i = 2; i <= n; ++i) {
    scanf("%d", &x);
    a[x + 1].push_back(i);
  }
  t = 0;
  dfs(1, 0);
  for (int z = 1; z <= hm; ++z) {
    for (int i = 0; i < b[z].size(); ++i) {
      x = T[b[z][i]];
      d.push(x);
      f[x] = g[x] = 1;
    }
    while (!d.empty()) {
      t = 0;
      y = fa[d.top()];
      f[y] = g[y] = 0;
      while (!d.empty()) {
        x = d.top();
        if (fa[x] != y)
          break;
        A[++t] = x;
        d.pop();
      }
      if (!y) {
        (ans += f[1] * H[n - b[z].size()] % mo) %= mo;
        break;
      }
      P[0] = 1;
      Q[t + 1] = 1;
      for (int i = 1; i <= t; ++i)
        P[i] = P[i - 1] * g[A[i]] % mo;
      for (int i = t; i; --i)
        Q[i] = Q[i + 1] * g[A[i]] % mo;
      for (int i = 1; i <= t; ++i)
        f[y] = (f[y] + P[i - 1] * Q[i + 1] % mo * f[A[i]] % mo) % mo;
      X = 1;
      for (int i = 1; i <= t; ++i)
        X = X * (f[A[i]] + g[A[i]]) % mo;
      g[y] = (X - f[y]) % mo;
      d.push(y);
    }
  }
  printf("%lld\n", (ans % mo + mo) % mo);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mo = 1000000007;
int hm, n, x, y, t, fa[200005], T[200005], e[200005], h[200005];
LL X, ans, P[200005], Q[200005], A[200005], f[200005], g[200005], H[200005];
vector<int> a[200005], b[200005];
struct cmp {
  bool operator()(int a, int b) {
    return h[a] == h[b] ? e[a] < e[b] : h[a] < h[b];
  }
};
priority_queue<int, vector<int>, cmp> d;
void dfs(int x, int y) {
  fa[x] = y;
  h[x] = h[y] + 1;
  hm = max(hm, h[x]);
  e[x] = ++t;
  if (!T[x])
    T[x] = x;
  b[h[x]].push_back(x);
  if (a[x].size() == 1)
    T[a[x][0]] = T[x];
  for (int i = 0; i < a[x].size(); ++i)
    dfs(a[x][i], x);
}
int main() {
  scanf("%d", &n);
  ++n;
  H[0] = 1;
  for (int i = 1; i <= n; ++i)
    H[i] = H[i - 1] * 2 % mo;
  for (int i = 2; i <= n; ++i) {
    scanf("%d", &x);
    a[x + 1].push_back(i);
  }
  t = 0;
  dfs(1, 0);
  for (int z = 1; z <= hm; ++z) {
    for (int i = 0; i < b[z].size(); ++i) {
      x = T[b[z][i]];
      d.push(x);
      f[x] = g[x] = 1;
    }
    while (!d.empty()) {
      t = 0;
      y = fa[d.top()];
      f[y] = g[y] = 0;
      while (!d.empty()) {
        x = d.top();
        if (fa[x] != y)
          break;
        A[++t] = x;
        d.pop();
      }
      if (!y) {
        (ans += f[1] * H[n - b[z].size()] % mo) %= mo;
        break;
      }
      P[0] = 1;
      Q[t + 1] = 1;
      for (int i = 1; i <= t; ++i)
        P[i] = P[i - 1] * g[A[i]] % mo;
      for (int i = t; i; --i)
        Q[i] = Q[i + 1] * g[A[i]] % mo;
      for (int i = 1; i <= t; ++i)
        f[y] = (f[y] + P[i - 1] * Q[i + 1] % mo * f[A[i]] % mo) % mo;
      X = 1;
      for (int i = 1; i <= t; ++i)
        X = X * (f[A[i]] + g[A[i]]) % mo;
      g[y] = (X - f[y]) % mo;
      f[T[y]] = f[y];
      g[T[y]] = g[y];
      d.push(T[y]);
    }
  }
  printf("%lld\n", (ans % mo + mo) % mo);
  return 0;
}