#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int mod = 1e9 + 7;
const int N = 1500000;
const int K = 20 ;
int p[N], anc[K][N], sz[N], f[N], t, dp[N][2];
vector < int > g[N], d[N];
vector < pair < int, pair < int, int > >  > e[N];
int gt(int x){
    if (x == p[x]) return x;
    return p[x] = gt(p[x]);
}
int binpow(int x, int y){
    int res = 1;
    while(y){
        if (y % 2) res = res * x % mod;
        x = x * x % mod;
        y /= 2;
    }
    return res;
}
int lca(int u, int v){
    for (int i = K - 1; i >= 0; i--){
        if (anc[i][u] != anc[i][v]){
            u = anc[i][u];
            v = anc[i][v];
        }
    }
    return anc[0][v];
}
void unite(int x, int y){
    x = gt(x);
    y = gt(y);
    if (x == y) return ;
    if (sz[x] < sz[y]) swap(x, y);
    p[y] = x;
    sz[x] += sz[y];
}
void dfs(int v, int h = 0){
    d[h].pb(v);
    f[v] = h;
    for (auto u: g[v]){
        dfs(u, h + 1);
    }
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
        int v;
        cin >> v;
        anc[0][i] = v;
        g[v].pb(i);
    }
    dfs(0);
    for (int i = 1; i < K; i++){
        for (int j = 1; j <= n; j++){
            anc[i][j] = anc[i - 1][anc[i - 1][j]];
        }
    }
    for (int i = 0; i <= n; i++){
        p[i] = i;
        sz[i] = 1;
        dp[i][0] = dp[i][1] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++){
        if (d[i].size() == 0) break;
        set < int > q;
        for (int j = 0; j < d[i].size(); j++){
            int u = d[i][j];
            int v = d[i][(j + 1) % d[i].size()];
            int par = lca(u, v);
            q.insert(f[u] - f[par]);
            e[f[u] - f[par]].pb({par, {u, v}});
        }
        for (auto j: q){
            sort(e[j].begin(), e[j].end());
            for (int l = 0; l < e[j].size();){
                auto x = e[j][l].F;
                int r = l;
                int val1 = 1;
                int val2 = 0;
                set < int > ns;
                ns.insert(gt(e[j][l].S.F));
                ns.insert(gt(e[j][l].S.S));
                while(r + 1 < e[j].size() && e[j][r + 1].F == x){
                    r++;
                    ns.insert(gt(e[j][r].S.F));
                    ns.insert(gt(e[j][r].S.S));
                }
                for (auto v: ns){
                    val1 = (val1 * dp[v][0]) % mod;
                    val2 = (val2 + binpow(dp[v][0], mod - 2) * dp[v][1]) % mod;
                }
                for (int k = l; k <= r; k++){
                    unite(e[j][k].S.F, e[j][k].S.S);
                }
                int v = gt(e[j][l].S.F);
                dp[v][1] = (val1 * val2) % mod;
                dp[v][0] = (binpow(2, sz[v]) - dp[v][1] + mod) % mod;
                l = r + 1;
            }
        }
        int v = gt(d[i][0]);
        ans = (ans + dp[v][1] * binpow(2, n + 1 - sz[v])) % mod;
       // cout << sz[v] << " " << d[i].size() << "  " << dp[v][1] << " " << v << " " << i << endl;
        for (auto j: q) e[j].clear();
    }
    cout << (ans + binpow(2, n)) % mod;
}


Measured memory_bytes: 216064