Problem p03499 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define S second
#define F first

using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
const int maxn = 2e5 + 20;
const int mod = 1e9 + 7;
set<pair<int, pii>> s[maxn];
vector<int> g[maxn];
vector<pii> mp[maxn];
int p2[maxn], depth[maxn], st[maxn];
int cc = 1;
int dfs(int u, int d) {
  depth[u] = d;
  int maxv = -1, mv = 0;
  for (int v : g[u]) {
    dfs(v, d + 1);

    if ((int)s[st[v]].size() > maxv) {
      //   cout << v << ' '  << s[st[v]].size() << endl;
      maxv = s[st[v]].size();
      mv = v;
    }
  }
  if (g[u].size() == 0) {
    st[u] = cc;
    cc++;
    s[st[u]].insert({d, {1, 1}});
    return 0;
  }
  st[u] = st[mv];
  set<int> st1;
  for (int v : g[u]) {
    if (v == mv)
      continue;
    for (pair<int, pii> p : s[st[v]]) {
      if (st1.find(p.F) == st1.end())
        mp[p.F].clear();
      st1.insert(p.F);
      mp[p.F].pb(p.S);
      auto it = s[st[u]].lower_bound(make_pair(p.F, make_pair(-1, -1)));
      if (it == s[st[u]].end() || it->F != p.F)
        continue;
      pair<int, pii> p1 = *it;
      s[st[u]].erase(p1);
      mp[p1.F].pb(p1.S);
    }
  }
  for (int a1 : st1) {
    vector<pii> v = mp[a1];
    int mul = 1;
    int a = 0;
    int s1 = 0;
    for (pii p : v) {
      s1 += p.S;
      a = (a * (p2[p.S] - p.F)) % mod;
      a = (a + (mul * p.F)) % mod;
      a %= mod;
      a += mod;
      a %= mod;
      mul = (mul * (p2[p.S] - p.F)) % mod;
      mul += mod;
      mul %= mod;
    }
    s[st[u]].insert({a1, {a, s1}});
  }
  s[st[u]].insert({d, {1, 1}});
  // cout << u << "?" << endl;
  // for (pair<int,pii> p : s[st[u]]){
  //         cout << p.F << ' ' << p.S.F << ' ' << p.S.S << endl;
  //      }
  //    cout << u << "!" << endl;
}
int32_t main() {
  freopen("in.in", "r", stdin);
  p2[0] = 1;
  for (int i = 1; i < maxn; i++) {
    p2[i] = (p2[i - 1] * 2) % mod;
  }
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    g[p].pb(i);
  }
  dfs(0, 0);
  int sum = 0;
  for (pair<int, pii> p : s[st[0]]) {
    // if (p.F < 10)
    // cout << p.F << ' '<< p.S.F << endl;
    sum = (sum + (p.S.F * p2[n + 1 - p.S.S])) % mod;
    sum %= mod;
  }
  sum %= mod;
  sum += mod;
  cout << sum % mod << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define S second
#define F first

using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
const int maxn = 2e5 + 20;
const int mod = 1e9 + 7;
set<pair<int, pii>> s[maxn];
vector<int> g[maxn];
vector<pii> mp[maxn];
int p2[maxn], depth[maxn], st[maxn];
int cc = 1;
int dfs(int u, int d) {
  depth[u] = d;
  int maxv = -1, mv = 0;
  for (int v : g[u]) {
    dfs(v, d + 1);

    if ((int)s[st[v]].size() > maxv) {
      //   cout << v << ' '  << s[st[v]].size() << endl;
      maxv = s[st[v]].size();
      mv = v;
    }
  }
  if (g[u].size() == 0) {
    st[u] = cc;
    cc++;
    s[st[u]].insert({d, {1, 1}});
    return 0;
  }
  st[u] = st[mv];
  set<int> st1;
  for (int v : g[u]) {
    if (v == mv)
      continue;
    for (pair<int, pii> p : s[st[v]]) {
      if (st1.find(p.F) == st1.end())
        mp[p.F].clear();
      st1.insert(p.F);
      mp[p.F].pb(p.S);
      auto it = s[st[u]].lower_bound(make_pair(p.F, make_pair(-1, -1)));
      if (it == s[st[u]].end() || it->F != p.F)
        continue;
      pair<int, pii> p1 = *it;
      s[st[u]].erase(p1);
      mp[p1.F].pb(p1.S);
    }
  }
  for (int a1 : st1) {
    vector<pii> v = mp[a1];
    int mul = 1;
    int a = 0;
    int s1 = 0;
    for (pii p : v) {
      s1 += p.S;
      a = (a * (p2[p.S] - p.F)) % mod;
      a = (a + (mul * p.F)) % mod;
      a %= mod;
      a += mod;
      a %= mod;
      mul = (mul * (p2[p.S] - p.F)) % mod;
      mul += mod;
      mul %= mod;
    }
    s[st[u]].insert({a1, {a, s1}});
  }
  s[st[u]].insert({d, {1, 1}});
  // cout << u << "?" << endl;
  // for (pair<int,pii> p : s[st[u]]){
  //         cout << p.F << ' ' << p.S.F << ' ' << p.S.S << endl;
  //      }
  //    cout << u << "!" << endl;
}
int32_t main() {
  // freopen("in.in","r",stdin);
  p2[0] = 1;
  for (int i = 1; i < maxn; i++) {
    p2[i] = (p2[i - 1] * 2) % mod;
  }
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p;
    g[p].pb(i);
  }
  dfs(0, 0);
  int sum = 0;
  for (pair<int, pii> p : s[st[0]]) {
    // if (p.F < 10)
    // cout << p.F << ' '<< p.S.F << endl;
    sum = (sum + (p.S.F * p2[n + 1 - p.S.S])) % mod;
    sum %= mod;
  }
  sum %= mod;
  sum += mod;
  cout << sum % mod << endl;
  return 0;
}