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: Accepted)

use std::io::*;
use std::str::FromStr;
use std::collections::VecDeque;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
fn pow_mod(base: i64, power: i64, modulo: i64) -> i64 {
    let mut base = base;
    let mut power = power;
    let mut ans = 1;
    while power > 0 {
        if power & 1 == 1 { ans = ans * base % modulo; }
        power >>= 1;
        base = base * base % modulo;
    }
    ans
}

#[allow(dead_code)]
fn inv_mod(a: i64, modulo: i64) -> i64 {
    pow_mod(a, modulo - 2, modulo)
}

type Weight = i32;
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
struct Edge {
    index: usize,
    src: usize,
    dest: usize,
    weight: Weight,
}
#[allow(dead_code)]
impl Edge {
    fn new(index: usize, src: usize, dest: usize, weight: Weight) -> Edge {
        Edge {
            index: index,
            src: src,
            dest: dest,
            weight: weight,
        }
    }
}
impl std::cmp::Ord for Edge {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (self.weight, self.src, self.dest).cmp(&(other.weight, other.src, other.dest))
    }
}
impl std::cmp::PartialOrd for Edge {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
type Edges = Vec<Edge>;
type Graph = Vec<Edges>;
#[allow(dead_code)]
fn graph_new(n: usize) -> Graph {
    vec![vec![]; n]
}
#[allow(dead_code)]
fn add_bi_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    graph[dest].push(Edge::new(*index, dest, src, weight));
    *index += 1;
}
#[allow(dead_code)]
fn add_uni_edge(graph: &mut Graph, index: &mut usize, src: usize, dest: usize, weight: Weight) {
    graph[src].push(Edge::new(*index, src, dest, weight));
    *index += 1;
}


const MOD: i64 = 1e+9 as i64 + 7;
struct Solver {
    n: usize,
    g: Graph,
}

impl Solver {
    fn new() -> Solver {
        let n: usize = read::<usize>() + 1;
        let mut g = graph_new(n);
        let mut e = 0;
        for i in 0..n-1 {
            let p: usize = read();
            add_bi_edge(&mut g, &mut e, p, i + 1, 1);
        }
        Solver {
            n: n,
            g: g,
        }
    }
    fn dfs(&self, from: usize, parent: usize) -> VecDeque<(i64, i64, i64)> {
        let mut deq = VecDeque::new();
        let mut twos = vec![];
        for edge in self.g[from].iter() {
            if edge.dest == parent { continue; }
            let mut ndeq = self.dfs(edge.dest, from);
            let mut swapped = false;
            if deq.len() < ndeq.len() {
                std::mem::swap(&mut deq, &mut ndeq);
                swapped = true;
            }
            if twos.len() < ndeq.len() {
                twos.resize(deq.len(), 0);
            }
            for i in 0..ndeq.len() {
                let zero = deq[i].0 * ndeq[i].0 % MOD;
                let one = (deq[i].0 * ndeq[i].1 + deq[i].1 * ndeq[i].0) % MOD;
                twos[i] = (deq[i].1 * ndeq[i].1 % MOD + twos[i] * 
                    if !swapped { ndeq[i].0 + ndeq[i].1 } else { deq[i].0 + deq[i].1 }
                % MOD ) % MOD;
                let cnt = deq[i].2 + ndeq[i].2;
                deq[i] = (zero, one, cnt);
            }
        }
        for i in 0..twos.len() {
            deq[i].0 = (deq[i].0 + twos[i]) % MOD;
        }
        deq.push_front((1, 1, 1));
        deq
    }
    fn solve(&mut self) {
        let mut pow2 = vec![1; self.n + 1];
        for i in 0..self.n {
            pow2[i + 1] = pow2[i] * 2 % MOD;
        }
        let deq = self.dfs(0, 0);
        let mut ans = 0;
        for tuple in deq.iter() {
            let cnt = tuple.1;
            let amp = pow2[self.n - tuple.2 as usize];
            ans = (ans + cnt * amp) % MOD;
        }
        println!("{}", ans);
    }
}

fn main() {
    let mut solver = Solver::new();
    solver.solve();
}