Problem:
Score : 1000 points

Problem Statement

Snuke has a rooted tree with N+1 vertices.
The vertices are numbered 0 through N, and Vertex 0 is the root of the tree.
The parent of Vertex i (1 \leq i \leq N) is Vertex p_i.

Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them.
The play begins with placing one marble on some of the vertices, then proceeds as follows:

If there is a marble on Vertex 0, move the marble into the box.

Move each marble from the vertex to its parent (all at once).

For each vertex occupied by two or more marbles, remove all the marbles from the vertex.

If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play.

There are 2^{N+1} ways to place marbles on some of the vertices.
For each of them, find the number of marbles that will be in the box at the end of the play, and compute the sum of all those numbers modulo 1,000,000,007.

Constraints

1 \leq N < 2 \times 10^{5}

0 \leq p_i < i

Partial Scores

In the test set worth 400 points, N < 2{,}000.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_{N}

Output

Print the answer.

Sample Input 1

2
0 0

Sample Output 1

8

When we place a marble on both Vertex 1 and 2, there will be multiple marbles on Vertex 0 by step 2. In such a case, these marbles will be removed instead of being moved to the box.

Sample Input 2

5
0 1 1 0 4

Sample Output 2

96

Sample Input 3

31
0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23

Sample Output 3

730395550

Be sure to compute the sum modulo 1,000,000,007.

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

struct TreeDP<Edge> {
    size: usize,
    edge: Vec<(usize, usize, Edge)>,
}

impl<Edge> TreeDP<Edge> {
    fn new(size: usize) -> Self {
        TreeDP {
            size: size,
            edge: vec![],
        }
    }
    fn add_edge(&mut self, a: usize, b: usize, c: Edge) {
        assert!(a < self.size && b < self.size && a != b);
        self.edge.push((a, b, c));
    }
    fn solve<Init, Merge, Value>(&self, root: usize, init: Init, merge: Merge) -> Value
    where
        Init: Fn(usize) -> Value,
        Merge: Fn(Value, Value, &Edge) -> Value,
    {
        let size = self.size;
        let mut graph = vec![vec![]; size];
        for (i, e) in self.edge.iter().enumerate() {
            graph[e.0].push((e.1, i));
            graph[e.1].push((e.0, i));
        }
        let mut topo = vec![];
        let mut stack = vec![(root, root)];
        while let Some((v, p)) = stack.pop() {
            topo.push(v);
            if let Some(k) = graph[v].iter().position(|e| e.0 == p) {
                graph[v].remove(k);
            }
            for e in graph[v].iter() {
                stack.push((e.0, v));
            }
        }
        assert!(topo.len() == size);
        let mut dp: Vec<Option<Value>> = (0..size).map(|_| None).collect();
        for &v in topo.iter().rev() {
            let mut now = init(v);
            for &(u, k) in graph[v].iter() {
                let b = dp[u].take().unwrap();
                now = merge(now, b, &self.edge[k].2);
            }
            dp[v] = Some(now);
        }
        dp[root].take().unwrap()
    }
}

use std::collections::VecDeque;
use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let mut solver = TreeDP::new(n + 1);
    for i in 0..n {
        let x: usize = it.next().unwrap().parse::<usize>().unwrap();
        solver.add_edge(x, i + 1, ());
    }
    const MOD: u64 = 1_000_000_007;
    type Value = VecDeque<(u64, u64, u64, usize)>;
    type Edge = ();
    let init = |_v: usize| -> Value {
        let mut deq = VecDeque::new();
        deq.push_back((1, 1, 0, 1));
        deq
    };
    let merge = |a: Value, b: Value, _e: &Edge| -> Value {
        let mut a = a;
        let mut b = b;
        if a.len() < b.len() + 1 {
            let v = a.pop_front().unwrap();
            b.push_front(v);
            std::mem::swap(&mut a, &mut b);
        }
        for i in 0..b.len() {
            let x = *b.get(i).unwrap();
            let y = *a.get(i + 1).unwrap();
            *a.get_mut(i + 1).unwrap() = (
                x.0 * (y.0 + y.2) % MOD,
                (x.1 * (y.0 + y.2) + x.0 * y.1) % MOD,
                (x.2 * (y.0 + y.1 + y.2) + x.1 * y.1) % MOD,
                x.3 + y.3,
            );
        }
        a
    };
    let mut res = solver.solve(0, init, merge);
    let mut pow = vec![1; n + 2];
    for i in 1..(n + 2) {
        pow[i] = pow[i - 1] * 2 % MOD;
    }
    let mut ans = 0;
    while let Some(x) = res.pop_front() {
        ans = (ans + x.1 * pow[n + 1 - x.3]) % MOD;
    }
    println!("{}", ans);
}

fn main() {
    run();
}