#include <cstdio>
#include <vector>
#include <cctype>

#define BUF_SIZE 1 << 16

int pos = BUF_SIZE;
char buf[BUF_SIZE];

inline char nextch() {
    if (pos == BUF_SIZE) fread(buf, BUF_SIZE, 1, stdin), pos = 0;
    return buf[pos++];
}

inline int read() {
    char ch;
    while (!isdigit(ch = nextch()));
    int x = ch - '0';
    while (isdigit(ch = nextch())) x = 10 * x + ch - '0';
    return x;
}

#define MOD 1000000007
#define MAXN 200009

std::vector < int > a[MAXN], g[MAXN];
struct myc {int x, y;} dp[MAXN];
bool term[MAXN], trim[MAXN], viz[MAXN];
int n, k, v[MAXN], t[MAXN], ans, put2[MAXN];
int stanga[MAXN], dreapta[MAXN];

inline void calc() {
    int cnt = 0;
    for (int i = 0; i < k; i++) {
        if (a[v[i]].size() == 0) {
            dp[v[i]].x = 1;
            dp[v[i]].y = 1;
            cnt++;
        } else {
            dp[v[i]].y = dp[v[i]].x = 0;
            int prod = 1;
            for (int j = 0; j < (int)a[v[i]].size(); j++) {
                dp[v[i]].y += dp[a[v[i]][j]].y;
                prod = 1LL * prod * (put2[dp[a[v[i]][j]].y] - dp[a[v[i]][j]].x + MOD) % MOD;
                stanga[j] = prod;
            }
            prod = 1;
            for (int j = a[v[i]].size() - 1; j >= 0; j--) {
                prod = 1LL * prod * (put2[dp[a[v[i]][j]].y] - dp[a[v[i]][j]].x + MOD) % MOD;
                dreapta[j] = prod;
            }
            for (int j = 0; j < (int)a[v[i]].size(); j++) {
                int left = 1, right = 1;
                if (j > 0)
                    left = stanga[j - 1];
                if (j + 1 < (int)a[v[i]].size())
                    right = dreapta[j + 1];
                dp[v[i]].x = (dp[v[i]].x + 1LL * dp[a[v[i]][j]].x * left % MOD * right) % MOD;
            }
            a[v[i]].clear();
        }
        if (v[i] != t[v[i]])
            a[t[v[i]]].push_back(v[i]);
    }
    ans = (ans + 1LL * dp[0].x * put2[n + 1 - cnt]) % MOD;
}

void dfs(int x) {
    if (term[x] == 1) {
        for (auto &y : g[x]) {
            viz[y] = 1;
            term[y] = 1;
            v[k++] = y;
        }
        term[x] = 0;
        if (g[x].size() > 1 || x == 0) {
            viz[x] = 1;
            trim[x] = 1;
            v[k++] = x;
        } else if (g[x].size() == 1) {
            trim[x] = 1;
            viz[x] = 0;
        } else {
            trim[x] = 0;
            viz[x] = 0;
        }
    } else {
        int cnt = 0;
        trim[x] = 0;
        for (auto &y : g[x]) {
            dfs(y);
            cnt += trim[y];
            trim[x] |= trim[y];
        }
        if (cnt > 1 || x == 0) {
            viz[x] = 1;
            v[k++] = x;
        } else viz[x] = 0;
    }
}

int main() {
    n = read();

    put2[0] = 1;
    for (int i = 1; i <= n + 1; i++)
        put2[i] = 2 * put2[i - 1] % MOD;

    for (int i = 1; i <= n; i++) {
        t[i] = read();
        g[t[i]].push_back(i);
    }

    k = 1;
    v[0] = 0;
    viz[0] = 1;
    term[0] = 1;
    while (k) {
        calc();
        k = 0;
        dfs(0);
        if (k == 1 && term[0] == 0)
            k = 0;
        for (int i = k - 1; i >= 0; i--)
            while (viz[t[v[i]]] == 0)
                t[v[i]] = t[t[v[i]]];
        for (int i = 0; i < k; i++)
            if (term[v[i]] == 0)
                g[v[i]].clear();
        for (int i = 0; i < k; i++)
            if (t[v[i]] != v[i])
                g[t[v[i]]].push_back(v[i]);
    }

    printf("%d\n", ans);

    return 0;
}


Measured memory_bytes: 20480