#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

long long pow_mod(long long x, long long n, long long mod_num) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) { res = (res * x) % mod_num; }
        x = (x * x) % mod_num;
        n = (n >> 1);
    }
    return res;
}

class Tree {
    public:
    struct Node {
        std::vector<int> edge;
        int rank;
    };
    std::vector<Node> node;
    Tree() {}
    Tree(const int node_num_max) {
        node.resize(node_num_max);
    }
    void add_directed_edge(const int from, const int to) {
        node[from].edge.push_back(to);
    }
    void add_undirected_edge(const int node1, const int node2) {
        add_directed_edge(node1, node2);
        add_directed_edge(node2, node1);
    }
    void set_rank(int n, int rank, int parent) { // 辺が有向でも無向でも使える。呼び出すとき、parentは-1にしておくとよい。
        node[n].rank = rank;
        for (int c : node[n].edge) {
            if (c != parent) {
                set_rank(c, rank + 1, n);
            }
        }
    }
};

const long long MOD = 1000000007;
const int MAX_N = 200050;
int N, p[MAX_N];

Tree tree = Tree(MAX_N);
long long prob[MAX_N] = {};

void merge(int n) {
    //std::cout << "merge_begin:" << n << std::endl;
    std::vector<int> child = tree.node[n].edge;
    std::vector<long long> value;
    int child_num = child.size();

    while(2 <= child_num) {
        long long base = 1;
        for (int c : child) {
            if (c != -1) {
                base = base * (MOD + 1 - prob[c]);
                base = base % MOD;
            }
        }
        long long v = 0;
        for (int c : child) {
            if (c != -1) {
                v = v + (base * ((pow_mod(MOD + 1 - prob[c], MOD - 2, MOD) * prob[c]) % MOD)) % MOD;
                v = v % MOD;
            }
        }
        value.push_back(v);

        for (int i = 0; i < child.size(); i++) {
            if (child[i] != -1) {
                if (tree.node[child[i]].edge.size() == 0) {
                    child[i] = -1;
                    child_num--;
                    if (child_num == 1) {
                        break;
                    }
                } else {
                    child[i] = tree.node[child[i]].edge[0];
                }
            }
        }
    }

    for (int i = 0; i < child.size(); i++) {
        if (child[i] != -1) {
            int c = tree.node[n].edge[i];
            tree.node[n].edge.clear();
            tree.node[n].edge.push_back(c);
            break;
        }
    }

    prob[n] = pow_mod(2, MOD - 2, MOD);
    for (int i = 0; i < value.size(); i++) {
        n = tree.node[n].edge[0];
        prob[n] = value[i];
    }

    //std::cout << "merge_end:" << n << std::endl;
}

int main(int argc, char **argv) {
    std::cin >> N;

    for (int i = 1; i <= N; i++) {
        std::cin >> p[i];
    }

    for (int i = 1; i <= N; i++) {
        tree.add_directed_edge(p[i], i);
    }

    tree.set_rank(0, 0, -1);

    std::vector<int> nodes;
    for (int i = 0; i <= N; i++) {
        nodes.push_back(i);
    }
    std::sort(nodes.begin(), nodes.end(), 
        [](const int a, const int b) { return tree.node[a].rank > tree.node[b].rank; }
    );

    for (int n : nodes) {
        merge(n);
    }

    long long ret = 0;
    int n = 0;
    while(true) {
        ret = (ret + prob[n]) % MOD;
        if (tree.node[n].edge.size() == 0) {
            break;
        }
        assert(tree.node[n].edge.size() == 1);
        n = tree.node[n].edge[0];
    }

    std::cout << (ret * pow_mod(2, N + 1, MOD)) % MOD << std::endl;

    return 0;

}



Measured memory_bytes: 21880