Problem p03499 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ran 2222
using namespace std;

/*#### modular_arithmetic */
class modular_arithmetic {
public:
  int mod, range;
  vector<int> fac, ifac;

  modular_arithmetic() { mod = 1000000007; }
  // No specific settings.
  void set_mod(int _mod) { mod = _mod; }
  int add(int x, int y) { return (x + y) % mod; }
  int sub(int x, int y) { return (x - y + mod) % mod; }
  int mul(int x, int y) { return (int)((long long int)x * y % mod); }
  int pw(int x, int y) {
    int r = 1;
    while (1) {
      if (y & 1)
        r = mul(r, x);
      if (y >>= 1)
        x = mul(x, x);
      else
        break;
    }
    return r;
  }
  // mod must be a prime.
  int inv(int x) { return pw(x, mod - 2); }
  int div(int x, int y) { return mul(x, inv(y)); }
  void init(int _range, int _mod = 1000000007) {
    range = min(_range, _mod);
    set_mod(_mod);
    fac.resize(range);
    ifac.resize(range);
    fac[0] = ifac[0] = 1;
    for (int i = 1; i < range; i++) {
      fac[i] = mul(fac[i - 1], i);
      ifac[i] = pw(fac[i], mod - 2);
    }
  }
  int comb(int x, int y) {
    if (x < y)
      return 0;
    return mul(fac[x], mul(ifac[y], ifac[x - y]));
  }
};
/*####*/
modular_arithmetic ma;

int n, m, dep[ran], maxdep[ran], cnt;
set<int> e[ran];
int d[ran][2];
void dfs(int x) {
  maxdep[x] = dep[x];
  for (auto y : e[x]) {
    dep[y] = dep[x] + 1;
    dfs(y);
    maxdep[x] = max(maxdep[x], maxdep[y]);
  }
}

int ace[ran];
int ancestor(int x) { return x - ace[x] ? ace[x] = ancestor(ace[x]) : x; }
int D;
void dfs2(int x) {
  if (dep[x] == D) {
    d[x][0] = 1;
    d[x][1] = 1;
    cnt++;
    return;
  }
  int part = 1, all = 1;
  d[x][1] = 0;
  set<int> to_erase;
  for (auto yy : e[x]) {
    int y = ancestor(yy);
    if (maxdep[y] < D) {
      to_erase.insert(yy);
      continue;
    }
    dfs2(y);
    part = ma.mul(part, d[y][0]);
    d[x][1] = ma.add(d[x][1], ma.div(d[y][1], d[y][0]));
    all = ma.mul(all, ma.add(d[y][0], d[y][1]));
  }
  d[x][1] = ma.mul(d[x][1], part);
  d[x][0] = ma.sub(all, d[x][1]);
  for (auto y : to_erase) {
    e[x].erase(y);
  }
  if (e[x].size() == 1)
    ace[x] = ancestor(*e[x].begin());
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    e[x].insert(i);
  }
  dep[0] = 0;
  dfs(0);
  for (int i = 0; i <= n; i++)
    ace[i] = i;
  int out = 0, m = maxdep[0];
  for (int i = 0; i <= m; i++) {
    cnt = 0;
    D = i;
    dfs2(ancestor(0));
    out = ma.add(out, ma.mul(ma.pw(2, n + 1 - cnt), d[ancestor(0)][1]));
  }
  printf("%d\n", out);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ran 202222
using namespace std;

/*#### modular_arithmetic */
class modular_arithmetic {
public:
  int mod, range;
  vector<int> fac, ifac;

  modular_arithmetic() { mod = 1000000007; }
  // No specific settings.
  void set_mod(int _mod) { mod = _mod; }
  int add(int x, int y) { return (x + y) % mod; }
  int sub(int x, int y) { return (x - y + mod) % mod; }
  int mul(int x, int y) { return (int)((long long int)x * y % mod); }
  int pw(int x, int y) {
    int r = 1;
    while (1) {
      if (y & 1)
        r = mul(r, x);
      if (y >>= 1)
        x = mul(x, x);
      else
        break;
    }
    return r;
  }
  // mod must be a prime.
  int inv(int x) { return pw(x, mod - 2); }
  int div(int x, int y) { return mul(x, inv(y)); }
  void init(int _range, int _mod = 1000000007) {
    range = min(_range, _mod);
    set_mod(_mod);
    fac.resize(range);
    ifac.resize(range);
    fac[0] = ifac[0] = 1;
    for (int i = 1; i < range; i++) {
      fac[i] = mul(fac[i - 1], i);
      ifac[i] = pw(fac[i], mod - 2);
    }
  }
  int comb(int x, int y) {
    if (x < y)
      return 0;
    return mul(fac[x], mul(ifac[y], ifac[x - y]));
  }
};
/*####*/
modular_arithmetic ma;

int n, m, dep[ran], maxdep[ran], cnt;
set<int> e[ran];
int d[ran][2];
void dfs(int x) {
  maxdep[x] = dep[x];
  for (auto y : e[x]) {
    dep[y] = dep[x] + 1;
    dfs(y);
    maxdep[x] = max(maxdep[x], maxdep[y]);
  }
}

int ace[ran];
int ancestor(int x) { return x - ace[x] ? ace[x] = ancestor(ace[x]) : x; }
int D;
void dfs2(int x) {
  if (dep[x] == D) {
    d[x][0] = 1;
    d[x][1] = 1;
    cnt++;
    return;
  }
  int part = 1, all = 1;
  d[x][1] = 0;
  set<int> to_erase;
  for (auto yy : e[x]) {
    int y = ancestor(yy);
    if (maxdep[y] < D) {
      to_erase.insert(yy);
      continue;
    }
    dfs2(y);
    part = ma.mul(part, d[y][0]);
    d[x][1] = ma.add(d[x][1], ma.div(d[y][1], d[y][0]));
    all = ma.mul(all, ma.add(d[y][0], d[y][1]));
  }
  d[x][1] = ma.mul(d[x][1], part);
  d[x][0] = ma.sub(all, d[x][1]);
  for (auto y : to_erase) {
    e[x].erase(y);
  }
  if (e[x].size() == 1)
    ace[x] = ancestor(*e[x].begin());
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    e[x].insert(i);
  }
  dep[0] = 0;
  dfs(0);
  for (int i = 0; i <= n; i++)
    ace[i] = i;
  int out = 0, m = maxdep[0];
  for (int i = 0; i <= m; i++) {
    cnt = 0;
    D = i;
    dfs2(ancestor(0));
    out = ma.add(out, ma.mul(ma.pw(2, n + 1 - cnt), d[ancestor(0)][1]));
  }
  printf("%d\n", out);
  return 0;
}