Problem p03499 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<int> G[200010];
const ll MOD = ll(1e9 + 7);

ll modpow(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll res = modpow(x, y / 2, m);
  return res * res % m * (y & 1 ? x : 1) % m;
}

int cnt[200010];

struct S {
  ll none, one, over;
  S() : none(1), one(1), over(0) {}
};

deque<S> dp[200010];

deque<S> *g(int v, int d) {
  cnt[d]++;

  if (G[v].size() == 0) {
    dp[v].push_back(S());
    return &dp[v];
  }

  deque<S> *ret = nullptr;
  for (auto c : G[v]) {
    auto res = g(c, d + 1);
    if (ret == nullptr)
      ret = res;
    else {
      if (ret->size() < res->size())
        swap(ret, res);

      for (int i = 0; i < res->size(); i++) {
        S s, s1 = (*ret)[i], s2 = (*res)[i];
        ll sum1 = 0, sum2 = 0;
        sum1 = (s1.none + s1.one + s1.over) % MOD;
        sum2 = (s2.none + s2.one + s2.over) % MOD;
        s.none = s1.none * s2.none % MOD;
        s.one = (s1.one * s2.none % MOD + s1.none * s2.one % MOD) % MOD;
        s.over = (sum1 * sum2 % MOD - s.none - s.one + 2 * MOD) % MOD;
        (*ret)[i] = s;
      }
      res->clear();
    }
  }
  for (int i = 0; i < ret->size(); i++) {
    (*ret)[i].none += (*ret)[i].over;
    (*ret)[i].none %= MOD;
    (*ret)[i].over = 0;
  }
  ret->push_front(S());
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int p;
    cin >> p;
    G[p].push_back(i);
  }

  deque<S> *res = g(0, 0);
  ll ans = 0;
  for (int i = 0; i < res->size(); i++) {
    if (cnt[i] == 0)
      continue;
    ans += (*res)[i].one * modpow(2, N + 1 - cnt[i], MOD) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<int> G[200010];
const ll MOD = ll(1e9 + 7);

ll modpow(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll res = modpow(x, y / 2, m);
  return res * res % m * (y & 1 ? x : 1) % m;
}

int cnt[200010];

struct S {
  ll none, one, over;
  S() : none(1), one(1), over(0) {}
};

deque<S> dp[200010];

deque<S> *g(int v, int d) {
  cnt[d]++;

  if (G[v].size() == 0) {
    dp[v].push_back(S());
    return &dp[v];
  }

  deque<S> *ret = nullptr;
  for (auto c : G[v]) {
    auto res = g(c, d + 1);
    if (ret == nullptr)
      ret = res;
    else {
      if (ret->size() < res->size())
        swap(ret, res);

      for (int i = 0; i < res->size(); i++) {
        S s, s1 = (*ret)[i], s2 = (*res)[i];
        ll sum1 = 0, sum2 = 0;
        sum1 = (s1.none + s1.one + s1.over) % MOD;
        sum2 = (s2.none + s2.one + s2.over) % MOD;
        s.none = s1.none * s2.none % MOD;
        s.one = (s1.one * s2.none % MOD + s1.none * s2.one % MOD) % MOD;
        s.over = (sum1 * sum2 % MOD - s.none - s.one + 2 * MOD) % MOD;
        (*ret)[i] = s;
      }
      res->clear();
    }
  }
  for (int i = 0; i < ret->size(); i++) {
    if ((*ret)[i].over == 0)
      break;
    (*ret)[i].none += (*ret)[i].over;
    (*ret)[i].none %= MOD;
    (*ret)[i].over = 0;
  }
  ret->push_front(S());
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int p;
    cin >> p;
    G[p].push_back(i);
  }

  deque<S> *res = g(0, 0);
  ll ans = 0;
  for (int i = 0; i < res->size(); i++) {
    if (cnt[i] == 0)
      continue;
    ans += (*res)[i].one * modpow(2, N + 1 - cnt[i], MOD) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}