// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else 
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
void sleep( double sec = 1021 ){
  clock_t s = clock();
  while( clock() - s < CLOCKS_PER_SEC * sec );
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
#define N 202020
int two[ N ];
void build(){
  two[ 0 ] = 1;
  for( int i = 1 ; i < N ; i ++ )
    two[ i ] = add( two[ i - 1 ] , two[ i - 1 ] );
}
vector<int> s[ N ];
int n , ans;
void init(){
  n = getint();
  for( int i = 1 ; i <= n ; i ++ )
    s[ getint() ].push_back( i );
}
#define K 18
vector<int> v[ N ];
int dep[ N ] , p[ K ][ N ] , mxdep;
inline int lca( int a , int b ){
  if( dep[ a ] > dep[ b ] ) swap( a , b );
  int dlt = dep[ b ] - dep[ a ];
  while( dlt ){
    int bt = __lg( dlt & (-dlt) );
    b = p[ bt ][ b ];
    dlt ^= (1 << bt);
  }
  if( a == b ) return a;
  for( int i = K - 1 ; i >= 0 ; i -- )
    if( p[ i ][ a ] != p[ i ][ b ] ){
      a = p[ i ][ a ];
      b = p[ i ][ b ];
    }
  return p[ 0 ][ a ];
}
void go( int now , int prt , int tdep ){
  dep[ now ] = tdep;
  p[ 0 ][ now ] = prt;
  v[ tdep ].push_back( now );
  mxdep = max( mxdep , tdep );
  for( int son : s[ now ] )
    go( son , now , tdep + 1 );
}
int tag[ N ] , stmp;
vector<int> nv[ N ];
void add_node( int x ){
  if( tag[ x ] == stmp ) return;
  tag[ x ] = stmp;
  nv[ x ].clear();
}
int build_tree( int ndep ){
  ++ stmp;
  vector<int> stk;
  for( int i : v[ ndep ] )
    nv[ i ].clear();
  for( int i : v[ ndep ] ){
    while( true ){
      if( stk.size() and lca( stk.back() , i ) == stk.back() )
        break;
      int stksz = stk.size();
      if( stksz <= 1 ) break;
      int nlca = lca( stk[ stksz - 2 ] , stk[ stksz - 1 ] );
      if( dep[ nlca ] >=
          dep[ lca( stk[ stksz - 1 ] , i ) ] ){
        add_node( nlca );
        if( stk[ stksz - 2 ] != nlca )
          nv[ nlca ].push_back( stk[ stksz - 2 ] );
        if( stk[ stksz - 1 ] != nlca )
          nv[ nlca ].push_back( stk[ stksz - 1 ] );
        stk.pop_back();
        stk.pop_back();
        stk.push_back( nlca );
      }else
        break;
    }
    stk.push_back( i );
  }
  while( stk.size() > 1u ){
    int stksz = stk.size();
    int nlca = lca( stk[ stksz - 2 ] , stk[ stksz - 1 ] );
    add_node( nlca );
    if( stk[ stksz - 2 ] != nlca )
      nv[ nlca ].push_back( stk[ stksz - 2 ] );
    if( stk[ stksz - 1 ] != nlca )
      nv[ nlca ].push_back( stk[ stksz - 1 ] );
    stk.pop_back();
    stk.pop_back();
    stk.push_back( nlca );
  }
  return stk[ 0 ];
}
int dp[ N ][ 2 ];
void go( int now ){
  sort( nv[ now ].begin() , nv[ now ].end() );
  nv[ now ].resize( unique( nv[ now ].begin() , nv[ now ].end() ) - nv[ now ].begin() );
  dp[ now ][ 0 ] = dp[ now ][ 1 ] = 0;
  if( nv[ now ].empty() ){
    dp[ now ][ 0 ] = dp[ now ][ 1 ] = 1;
    return;
  }
  int all = 1 , got1 = 0;
  vector<int> dl , dr;
  for( int ss : nv[ now ] ){
    go( ss );
    all = mul( all , add( dp[ ss ][ 0 ] , dp[ ss ][ 1 ] ) );
    dl.push_back( dp[ ss ][ 0 ] );
    dr.push_back( dp[ ss ][ 0 ] );
  }
  for( size_t i = 1 ; i < dl.size() ; i ++ )
    dl[ i ] = mul( dl[ i ] , dl[ i - 1 ] );
  for( int i = (int)dr.size() - 2 ; i >= 0 ; i -- )
    dr[ i ] = mul( dr[ i ] , dr[ i + 1 ] );
  for( size_t i = 0 ; i < nv[ now ].size() ; i ++ ){
    int bns = 1;
    if( i ) bns = mul( bns , dl[ i - 1 ] );
    if( i + 1 < nv[ now ].size() )
      bns = mul( bns , dr[ i + 1 ] );
    got1 = add( got1 , mul( bns , dp[ nv[ now ][ i ] ][ 1 ] ) );
  }
  dp[ now ][ 0 ] = sub( all , got1 );
  dp[ now ][ 1 ] = got1;
}
void calc( int ndep ){
  if( v[ ndep ].size() == 0u ) return;
  int bns = two[ n + 1 - (int)v[ ndep ].size() ];
  if( v[ ndep ].size() == 1u ){
    ans = add( ans , bns );
    return;
  }
  int root = build_tree( ndep );
  go( root );
  ans = add( ans , mul( bns , dp[ root ][ 1 ] ) );
}
void solve(){
  go( 0 , 0 , 0 );
  for( int j = 1 ; j < K ; j ++ )
    for( int i = 0 ; i <= n ; i ++ )
      p[ j ][ i ] = p[ j - 1 ][ p[ j - 1 ][ i ] ];
  for( int i = 0 ; i <= mxdep ; i ++ )
    calc( i );
  printf( "%d\n" , ans );
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


Measured memory_bytes: 54272