Problem p03499 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <vector>
#define sqr(x) (x) * (x)
using namespace std;
deque<long long> dp[100005][3];
long long n, m, i, j, fa[100005], mod = 1e9 + 7, ans, mxd, f[100005],
                                  cnt[100005];
vector<long long> bi[100005];
void dfs(long long x, long long dep) {
  cnt[dep]++;
  mxd = max(mxd, dep);
  int mxs = 0, i, j;
  for (i = 0; i < bi[x].size(); i++) {
    dfs(bi[x][i], dep + 1);
    if (dp[x][0].size() < dp[bi[x][i]][0].size()) {
      swap(dp[x][0], dp[bi[x][i]][0]);
      swap(dp[x][1], dp[bi[x][i]][1]);
      swap(dp[x][2], dp[bi[x][i]][2]);
    }
    mxs = max(mxs, (int)dp[bi[x][i]][0].size());
    for (j = 0; j < dp[bi[x][i]][0].size(); j++) {
      f[0] = dp[x][0][j] * dp[bi[x][i]][0][j] % mod;
      f[1] = dp[x][0][j] * dp[bi[x][i]][1][j] % mod +
             dp[x][1][j] * dp[bi[x][i]][0][j] % mod;
      f[1] %= mod;
      f[2] = ((((dp[x][0][j] + dp[x][1][j] + dp[x][2][j]) % mod) *
                   ((dp[bi[x][i]][0][j] + dp[bi[x][i]][1][j] +
                     dp[bi[x][i]][2][j]) %
                    mod) %
                   mod -
               f[1] - f[0]) %
                  mod +
              mod) %
             mod;
      dp[x][0][j] = f[0];
      dp[x][1][j] = f[1];
      dp[x][2][j] = f[2];
    }
    dp[bi[x][i]][0].clear();
    dp[bi[x][i]][1].clear();
    dp[bi[x][i]][2].clear();
  }
  dp[x][0].push_front(1);
  dp[x][1].push_front(1);
  dp[x][2].push_front(0);
  for (i = 0; i <= mxs; i++) {
    dp[x][0][i] += dp[x][2][i];
    dp[x][2][i] = 0;
  }
  //	cerr<<x<<":"<<endl;
  //	for (i=0;i<=mxs;i++) cerr<<dp[x][0][i]<<' '<<dp[x][1][i]<<endl;
}
long long qp(long long x, long long y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  long long z = qp(x, y / 2);
  z = z * z % mod;
  if (y & 1)
    return z * x % mod;
  else
    return z;
}
int main() {
  cin >> n;
  n++;
  for (i = 2; i <= n; i++) {
    cin >> fa[i];
    fa[i]++;
    bi[fa[i]].push_back(i);
  }
  // cnt[1]=1;
  dfs(1, 0);
  for (i = 0; i <= mxd; i++) {
    ans += dp[1][1][i] * qp(2, n - cnt[i]);
    ans %= mod;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <vector>
#define sqr(x) (x) * (x)
using namespace std;
deque<long long> dp[200005][3];
long long n, m, i, j, fa[200005], mod = 1e9 + 7, ans, mxd, f[200005],
                                  cnt[200005];
vector<long long> bi[200005];
void dfs(long long x, long long dep) {
  cnt[dep]++;
  mxd = max(mxd, dep);
  int mxs = 0, i, j;
  for (i = 0; i < bi[x].size(); i++) {
    dfs(bi[x][i], dep + 1);
    if (dp[x][0].size() < dp[bi[x][i]][0].size()) {
      swap(dp[x][0], dp[bi[x][i]][0]);
      swap(dp[x][1], dp[bi[x][i]][1]);
      swap(dp[x][2], dp[bi[x][i]][2]);
    }
    mxs = max(mxs, (int)dp[bi[x][i]][0].size());
    for (j = 0; j < dp[bi[x][i]][0].size(); j++) {
      f[0] = dp[x][0][j] * dp[bi[x][i]][0][j] % mod;
      f[1] = dp[x][0][j] * dp[bi[x][i]][1][j] % mod +
             dp[x][1][j] * dp[bi[x][i]][0][j] % mod;
      f[1] %= mod;
      f[2] = ((((dp[x][0][j] + dp[x][1][j] + dp[x][2][j]) % mod) *
                   ((dp[bi[x][i]][0][j] + dp[bi[x][i]][1][j] +
                     dp[bi[x][i]][2][j]) %
                    mod) %
                   mod -
               f[1] - f[0]) %
                  mod +
              mod) %
             mod;
      dp[x][0][j] = f[0];
      dp[x][1][j] = f[1];
      dp[x][2][j] = f[2];
    }
    dp[bi[x][i]][0].clear();
    dp[bi[x][i]][1].clear();
    dp[bi[x][i]][2].clear();
  }
  dp[x][0].push_front(1);
  dp[x][1].push_front(1);
  dp[x][2].push_front(0);
  for (i = 0; i <= mxs; i++) {
    dp[x][0][i] += dp[x][2][i];
    dp[x][2][i] = 0;
  }
  //	cerr<<x<<":"<<endl;
  //	for (i=0;i<=mxs;i++) cerr<<dp[x][0][i]<<' '<<dp[x][1][i]<<endl;
}
long long qp(long long x, long long y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x;
  long long z = qp(x, y / 2);
  z = z * z % mod;
  if (y & 1)
    return z * x % mod;
  else
    return z;
}
int main() {
  cin >> n;
  n++;
  for (i = 2; i <= n; i++) {
    cin >> fa[i];
    fa[i]++;
    bi[fa[i]].push_back(i);
  }
  // cnt[1]=1;
  dfs(1, 0);
  for (i = 0; i <= mxd; i++) {
    ans += dp[1][1][i] * qp(2, n - cnt[i]);
    ans %= mod;
  }
  cout << ans;
  return 0;
}