Problem p03499 (C++)

Buggy submission (status: Runtime Error):
/*
 /+==================================================+\
//+--------------------------------------------------+\\
|.|\\...>>>>>>> Hollwo_Pelw(ass) 's code <<<<<<<...//|.|
\\+--------------------------------------------------+//
 \+==================================================+/
*/
#include <bits/stdc++.h>
using namespace std;
// type
typedef long long ll;
typedef long double ld;
// loop
// #define For(i, l, r)        for (int i = l; i < r; i++)
// #define ForE(i, l, r)       for (int i = l; i <= r; i++)
// #define Ford(i, r, l)       for (int i = r; i > l; i--)
// #define FordE(i, r, l)      for (int i = r; i >= l; i--)
// #define Fora(i, a)          for (auto i : a)
// I/O
#define open(file, in, out)                                                    \
  if (fopen(file in, "r")) {                                                   \
    freopen(file in, "r", stdin);                                              \
    freopen(file out, "w", stdout);                                            \
  }
#define FAST_IO                                                                \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define setpre(n) fixed << setprecision(n)
// pair
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
// vector & !!?(string)
#define eb emplace_back
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) a.size()
// geometry calculate
#define pi acos(-1.0)
#define g_sin(a) sin(a *pi / 180)
#define g_cos(a) cos(a *pi / 180)
#define g_tan(a) tan(a *pi / 180)
// set val
#define ms0(a) memset(a, 0, sizeof(a));
#define ms1(a) memset(a, 1, sizeof(a));
#define msn1(a) memset(a, -1, sizeof(a));
#define msinf(a) memset(a, 0x3f3f3f, sizeof(a));
// constant
const int mod1 = 998244353, mod = 1e9 + 7;
const int MAXN = 100005, MAXM = 200010, VV = 5e8 + 4;
// code
#define int long long

int n, p, sz[MAXN], w[MAXN];
int ans;
vector<int> adj[MAXN];
deque<pii> dp[MAXN];
void merge(pii &a, pii &b) {
  a.F = (a.F * b.S + a.S * b.F) % mod;
  a.S = a.S * b.S % mod;
}
void rev(pii &a) { a.S = (mod - a.F + 1) % mod; }
void dfs(int u) {
  int f = w[u] = u, s = 0;
  for (int &nx : adj[u]) {
    dfs(nx);
    if (sz[f] < sz[nx]) {
      s = sz[f];
      swap(f, nx);
    } else if (s < sz[nx]) {
      s = sz[nx];
    }
    for (int i = 0; i < sz[nx]; i++)
      merge(dp[w[f]][i], dp[w[nx]][i]);
  }
  for (int i = 0; i < s; i++)
    rev(dp[w[f]][i]);
  dp[w[f]].push_front({VV, VV});

  w[u] = w[f];
  sz[u] = sz[f] + 1;
}

void Solve() {
  // cout << (int) (5e8+4) * (int) (5e8+4) % mod;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p;
    adj[p].push_back(i);
  }
  dfs(0);
  for (auto t : dp[w[0]])
    ans += t.F;
  ans %= mod;
  // cout << ans << endl;
  int k = n + 1, p = 2;
  while (k) {
    if (k & 1)
      (ans *= p) %= mod;
    (p *= p) %= mod;
    k /= 2;
  }
  cout << ans << endl;
}

signed main() {
  open("", ".inp", ".out");
  FAST_IO;
  int TC = 1;
  // cin >> TC;
  while (TC--)
    Solve();
  return 0;
}
/*
./-=====>>><<<-------- DEBUG -------->>><<<=====-\.
/.................................................\

+====================== INP ======================+


+====================== OUT ======================+


\................................................./
.\-=====>>><<<--------= END =-------->>><<<=====-/.
*/


Fixed submission:
/*
 /+==================================================+\
//+--------------------------------------------------+\\
|.|\\...>>>>>>> Hollwo_Pelw(ass) 's code <<<<<<<...//|.|
\\+--------------------------------------------------+//
 \+==================================================+/
*/
#include <bits/stdc++.h>
using namespace std;
// type
typedef long long ll;
typedef long double ld;
// loop
// #define For(i, l, r)        for (int i = l; i < r; i++)
// #define ForE(i, l, r)       for (int i = l; i <= r; i++)
// #define Ford(i, r, l)       for (int i = r; i > l; i--)
// #define FordE(i, r, l)      for (int i = r; i >= l; i--)
// #define Fora(i, a)          for (auto i : a)
// I/O
#define open(file, in, out)                                                    \
  if (fopen(file in, "r")) {                                                   \
    freopen(file in, "r", stdin);                                              \
    freopen(file out, "w", stdout);                                            \
  }
#define FAST_IO                                                                \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define setpre(n) fixed << setprecision(n)
// pair
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
// vector & !!?(string)
#define eb emplace_back
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) a.size()
// geometry calculate
#define pi acos(-1.0)
#define g_sin(a) sin(a *pi / 180)
#define g_cos(a) cos(a *pi / 180)
#define g_tan(a) tan(a *pi / 180)
// set val
#define ms0(a) memset(a, 0, sizeof(a));
#define ms1(a) memset(a, 1, sizeof(a));
#define msn1(a) memset(a, -1, sizeof(a));
#define msinf(a) memset(a, 0x3f3f3f, sizeof(a));
// constant
const int mod1 = 998244353, mod = 1e9 + 7;
const int MAXN = 2e5 + 10, MAXM = 1e5 + 5, VV = 5e8 + 4;
// code
#define int long long

int n, p, sz[MAXN], w[MAXN];
int ans;
vector<int> adj[MAXN];
deque<pii> dp[MAXN];
void merge(pii &a, pii &b) {
  a.F = (a.F * b.S + a.S * b.F) % mod;
  a.S = a.S * b.S % mod;
}
void rev(pii &a) { a.S = (mod - a.F + 1) % mod; }
void dfs(int u) {
  int f = w[u] = u, s = 0;
  for (int &nx : adj[u]) {
    dfs(nx);
    if (sz[f] < sz[nx]) {
      s = sz[f];
      swap(f, nx);
    } else if (s < sz[nx]) {
      s = sz[nx];
    }
    for (int i = 0; i < sz[nx]; i++)
      merge(dp[w[f]][i], dp[w[nx]][i]);
  }
  for (int i = 0; i < s; i++)
    rev(dp[w[f]][i]);
  dp[w[f]].push_front({VV, VV});

  w[u] = w[f];
  sz[u] = sz[f] + 1;
}

void Solve() {
  // cout << (int) (5e8+4) * (int) (5e8+4) % mod;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p;
    adj[p].push_back(i);
  }
  dfs(0);
  for (auto t : dp[w[0]])
    ans += t.F;
  ans %= mod;
  // cout << ans << endl;
  int k = n + 1, p = 2;
  while (k) {
    if (k & 1)
      (ans *= p) %= mod;
    (p *= p) %= mod;
    k /= 2;
  }
  cout << ans << endl;
}

signed main() {
  open("", ".inp", ".out");
  FAST_IO;
  int TC = 1;
  // cin >> TC;
  while (TC--)
    Solve();
  return 0;
}
/*
./-=====>>><<<-------- DEBUG -------->>><<<=====-\.
/.................................................\

+====================== INP ======================+


+====================== OUT ======================+


\................................................./
.\-=====>>><<<--------= END =-------->>><<<=====-/.
*/