Problem p03499 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int N = 20005;
const long long mod = 1e9 + 7;
int n, h[N], cnt, x, id[N], tg[N], ans;
long long fac[N];
vector<int> de[N];
struct qwe {
  long long x0, x1, x2;
};
typedef deque<qwe> di;
di d[N];
struct qw {
  int ne, to;
} e[N];
void add(int u, int v) { // cout<<u<<" "<<v<<endl;
  cnt++;
  e[cnt].ne = h[u];
  e[cnt].to = v;
  h[u] = cnt;
}
void pre(int u, int dep) { // cout<<u<<endl;
  de[dep].push_back(u);
  for (int i = h[u]; i; i = e[i].ne)
    pre(e[i].to, dep + 1);
}
void hb(di &a, di &b, int &ta, int &tb, int cnt) {
  if (a.size() == 0 || b.size() == 0)
    return;
  qwe x = a.front();
  a.pop_front();
  qwe y = b.front();
  b.pop_front();
  if (cnt == ta) {
    ++ta;
    x.x0 = (x.x0 + x.x2) % mod;
    x.x2 = 0;
  }
  if (cnt == tb) {
    ++tb;
    y.x0 = (y.x0 + y.x2) % mod;
    y.x2 = 0;
  }
  hb(a, b, ta, tb, cnt + 1);
  qwe c;
  c.x0 = x.x0 * y.x0 % mod;
  c.x1 = (x.x0 * y.x1 % mod + x.x1 * y.x0 % mod) % mod;
  c.x2 = (x.x0 * y.x2 % mod + x.x1 * y.x2 % mod + x.x2 * y.x2 % mod +
          x.x1 * y.x1 % mod + x.x2 * y.x1 % mod + x.x2 * y.x0 % mod) %
         mod;
  if (a.size() > b.size())
    a.push_front(c);
  else
    b.push_front(c);
}
void dfs(int u) {
  int t = n + 1;
  for (int i = h[u]; i; i = e[i].ne) {
    dfs(e[i].to);
    hb(d[t], d[id[e[i].to]], tg[t], tg[id[e[i].to]], 0);
    if (d[t].size() < d[id[e[i].to]].size())
      t = id[e[i].to];
  }
  if (t == n + 1)
    t = u;
  qwe s;
  s.x0 = s.x1 = 1, s.x2 = 0;
  id[u] = t;
  d[t].push_front(s);
  tg[t] = 0;
}
int main() {
  scanf("%d", &n);
  n++;
  for (int i = 1; i < n; i++) {
    scanf("%d", &x);
    add(x, i);
  }
  fac[0] = 1ll;
  for (int i = 1; i <= n + 1; i++)
    fac[i] = fac[i - 1] * 2ll % mod;
  pre(0, 0); // cout<<"????";
  dfs(0);
  di &s = d[id[0]];
  int now = 0;
  while (!s.empty()) {
    long long k = s.front().x1;
    s.pop_front();
    ans = (ans + fac[n - de[now].size()] * k % mod) % mod;
    ++now;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int N = 500005;
const long long mod = 1e9 + 7;
int n, h[N], cnt, x, id[N], tg[N], ans;
long long fac[N];
vector<int> de[N];
struct qwe {
  long long x0, x1, x2;
};
typedef deque<qwe> di;
di d[N];
struct qw {
  int ne, to;
} e[N];
void add(int u, int v) { // cout<<u<<" "<<v<<endl;
  cnt++;
  e[cnt].ne = h[u];
  e[cnt].to = v;
  h[u] = cnt;
}
void pre(int u, int dep) { // cout<<u<<endl;
  de[dep].push_back(u);
  for (int i = h[u]; i; i = e[i].ne)
    pre(e[i].to, dep + 1);
}
void hb(di &a, di &b, int &ta, int &tb, int cnt) {
  if (a.size() == 0 || b.size() == 0)
    return;
  qwe x = a.front();
  a.pop_front();
  qwe y = b.front();
  b.pop_front();
  if (cnt == ta) {
    ++ta;
    x.x0 = (x.x0 + x.x2) % mod;
    x.x2 = 0;
  }
  if (cnt == tb) {
    ++tb;
    y.x0 = (y.x0 + y.x2) % mod;
    y.x2 = 0;
  }
  hb(a, b, ta, tb, cnt + 1);
  qwe c;
  c.x0 = x.x0 * y.x0 % mod;
  c.x1 = (x.x0 * y.x1 % mod + x.x1 * y.x0 % mod) % mod;
  c.x2 = (x.x0 * y.x2 % mod + x.x1 * y.x2 % mod + x.x2 * y.x2 % mod +
          x.x1 * y.x1 % mod + x.x2 * y.x1 % mod + x.x2 * y.x0 % mod) %
         mod;
  if (a.size() > b.size())
    a.push_front(c);
  else
    b.push_front(c);
}
void dfs(int u) {
  int t = n + 1;
  for (int i = h[u]; i; i = e[i].ne) {
    dfs(e[i].to);
    hb(d[t], d[id[e[i].to]], tg[t], tg[id[e[i].to]], 0);
    if (d[t].size() < d[id[e[i].to]].size())
      t = id[e[i].to];
  }
  if (t == n + 1)
    t = u;
  qwe s;
  s.x0 = s.x1 = 1, s.x2 = 0;
  id[u] = t;
  d[t].push_front(s);
  tg[t] = 0;
}
int main() {
  scanf("%d", &n);
  n++;
  for (int i = 1; i < n; i++) {
    scanf("%d", &x);
    add(x, i);
  }
  fac[0] = 1ll;
  for (int i = 1; i <= n + 1; i++)
    fac[i] = fac[i - 1] * 2ll % mod;
  pre(0, 0); // cout<<"????";
  dfs(0);
  di &s = d[id[0]];
  int now = 0;
  while (!s.empty()) {
    long long k = s.front().x1;
    s.pop_front();
    ans = (ans + fac[n - de[now].size()] * k % mod) % mod;
    ++now;
  }
  printf("%lld\n", ans);
  return 0;
}