Problem p03499 (C++)

Buggy submission (status: Time Limit Exceeded):
// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \
  rep(_, v.size()) cout << " " << v[_];                                        \
  cout << endl;
#define show2d(v)                                                              \
  rep(_, v.size()) {                                                           \
    rep(__, v[_].size()) cout << " " << v[_][__];                              \
    cout << endl;                                                              \
  }                                                                            \
  cout << endl;
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 1e18 + 10 : 1e9 + 10;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; // RDLU
int n;
vvi G;
vi deg(200010);
#define MOD 1000000007
// a^b mod MOD
ll powmod(ll a, ll b, ll m = MOD) {
  ll out = 1;
  ll p = a % m;
  while (b) {
    if (b & 1)
      out = out * p % m;
    p = p * p % m;
    b >>= 1;
  }
  return out;
}
vi merge(vi a, vi b) {
  vi c(3);
  rep(i, 3) rep(j, 3)(c[i + j >= 2 ? 2 : i + j] += a[i] * b[j]) %= MOD;
  return c;
}
deque<vi> f(int a, int pr, int d) {
  deg[d]++;
  deque<vi> out;
  int ma = 0;
  rep(i, G[a].size()) {
    int to = G[a][i];
    if (to == pr)
      continue;
    deque<vi> t = f(to, a, d + 1), tmp;
    ma = max(ma, (int)min(out.size(), t.size()));
    rep(j, min(out.size(), t.size())) out[j] = t[j] = merge(out[j], t[j]);
    if (out.size() < t.size())
      out = t;
  }
  rep(i, ma) out[i][0] += out[i][2], out[i][2] = 0;
  out.push_front({1, 1, 0});
  return out;
}
signed main() {
  cin >> n;
  G = vvi(n + 1);
  rep(i, n) {
    int a;
    cin >> a;
    G[a].pb(i + 1);
  }
  deque<vi> out = f(0, -1, 0);
  int sum = 0;
  rep(i, out.size())(sum += out[i][1] * powmod(2, n + 1 - deg[i])) %= MOD;
  cout << sum << endl;
}


Fixed submission:
// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \
  rep(_, v.size()) cout << " " << v[_];                                        \
  cout << endl;
#define show2d(v)                                                              \
  rep(_, v.size()) {                                                           \
    rep(__, v[_].size()) cout << " " << v[_][__];                              \
    cout << endl;                                                              \
  }                                                                            \
  cout << endl;
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 1e18 + 10 : 1e9 + 10;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; // RDLU
int n;
vvi G;
vi deg(200010);
#define MOD 1000000007
// a^b mod MOD
ll powmod(ll a, ll b, ll m = MOD) {
  ll out = 1;
  ll p = a % m;
  while (b) {
    if (b & 1)
      out = out * p % m;
    p = p * p % m;
    b >>= 1;
  }
  return out;
}
vi merge(vi a, vi b) {
  vi c(3);
  rep(i, 3) rep(j, 3)(c[i + j >= 2 ? 2 : i + j] += a[i] * b[j]) %= MOD;
  return c;
}
deque<vi> f(int a, int pr, int d) {
  deg[d]++;
  deque<vi> out;
  int ma = 0;
  rep(i, G[a].size()) {
    int to = G[a][i];
    if (to == pr)
      continue;
    deque<vi> t = f(to, a, d + 1), tmp;
    ma = max(ma, (int)min(out.size(), t.size()));
    rep(j, min(out.size(), t.size())) out[j] = t[j] = merge(out[j], t[j]);
    if (out.size() < t.size())
      swap(out, t);
  }
  rep(i, ma) out[i][0] += out[i][2], out[i][2] = 0;
  out.push_front({1, 1, 0});
  return out;
}
signed main() {
  cin >> n;
  G = vvi(n + 1);
  rep(i, n) {
    int a;
    cin >> a;
    G[a].pb(i + 1);
  }
  deque<vi> out = f(0, -1, 0);
  int sum = 0;
  rep(i, out.size())(sum += out[i][1] * powmod(2, n + 1 - deg[i])) %= MOD;
  cout << sum << endl;
}