#include <bits/stdc++.h>
using namespace std;
int n;
vector < int > e[ 1001 ] ;
vector < int > nonTree_A;
vector < int > nonTree_B;
long long mod = 1000000007 ;
bool mustTake[ 1001 ] ;
bool mustNotTake[ 1001 ] ;
struct minimalAndWays
{
long long minimal;
long long ways;
minimalAndWays( )
{
minimal = 1000000000 ;
ways = 0 ;
}
minimalAndWays( long long _m, long long _w)
{
minimal = _m;
ways = _w;
}
minimalAndWays add( const minimalAndWays & that) const
{
return minimalAndWays( minimal + that.minimal , ( ways * that.ways ) % mod) ;
}
minimalAndWays update( const minimalAndWays & that) const
{
if ( minimal < that.minimal )
return * this ;
if ( minimal > that.minimal )
return that;
return minimalAndWays( minimal, ( ways + that.ways ) % mod) ;
}
} inf, zero( 0 , 1 ) ;
long long calls = 0 ;
bool done[ 1001 ] [ 2 ] ;
minimalAndWays mem[ 1001 ] [ 2 ] ;
minimalAndWays dp( int cur, int from, int takeThis)
{
calls ++ ;
if ( mustTake[ cur] && ! takeThis) return inf;
if ( mustNotTake[ cur] && takeThis) return inf;
if ( done[ cur] [ takeThis] ) return mem[ cur] [ takeThis] ;
minimalAndWays ret = zero;
for ( int i = 0 ; i < e[ cur] .size ( ) ; i++ )
{
int t = e[ cur] [ i] ;
if ( t == from) continue ;
minimalAndWays w = dp( t, cur, 1 ) ;
if ( takeThis)
w = w.update ( dp( t, cur, 0 ) ) ;
ret = ret.add ( w) ;
}
if ( takeThis)
ret.minimal + = 1 ;
done[ cur] [ takeThis] = true ;
mem[ cur] [ takeThis] = ret;
return ret;
}
class VampireTreeDiv2
{
public : int countMinSamples( vector < int > A, vector < int > B)
{
n = A.size ( ) + 1 ;
for ( int i = 1 ; i < n; i++ )
{
e[ i] .push_back ( A[ i- 1 ] ) ;
e[ A[ i- 1 ] ] .push_back ( i) ;
if ( B[ i- 1 ] ! = - 1 )
{
nonTree_A.push_back ( i) ;
nonTree_B.push_back ( B[ i- 1 ] ) ;
}
}
minimalAndWays ret;
for ( int mask = 0 ; mask < ( 1 << ( nonTree_A.size ( ) ) ) ; mask ++ )
{
memset ( mustTake, false , sizeof ( mustTake) ) ;
memset ( mustNotTake, false , sizeof ( mustNotTake) ) ;
memset ( done, false , sizeof ( done) ) ;
for ( int i = 0 ; i < nonTree_A.size ( ) ; i++ )
if ( ( mask & ( 1 << i) ) > 0 )
mustTake[ nonTree_A[ i] ] = true ;
else
{
mustTake[ nonTree_B[ i] ] = true ;
mustNotTake[ nonTree_A[ i] ] = true ;
}
ret = ret.update ( dp( 0 , - 1 , 0 ) ) ;
ret = ret.update ( dp( 0 , - 1 , 1 ) ) ;
}
return ret.ways ;
}
} ;
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbjsKdmVjdG9yIDxpbnQ+IGVbMTAwMV07CnZlY3RvciA8aW50PiBub25UcmVlX0E7CnZlY3RvciA8aW50PiBub25UcmVlX0I7CmxvbmcgbG9uZyBtb2QgPSAxMDAwMDAwMDA3Owpib29sIG11c3RUYWtlWzEwMDFdOwpib29sIG11c3ROb3RUYWtlWzEwMDFdOwoKc3RydWN0IG1pbmltYWxBbmRXYXlzCnsKCWxvbmcgbG9uZyBtaW5pbWFsOwoJbG9uZyBsb25nIHdheXM7CgltaW5pbWFsQW5kV2F5cygpCgl7CgkJbWluaW1hbCA9IDEwMDAwMDAwMDA7CgkJd2F5cyA9IDA7Cgl9CgltaW5pbWFsQW5kV2F5cyhsb25nIGxvbmcgX20sIGxvbmcgbG9uZyBfdykKCXsKCQltaW5pbWFsID0gX207CgkJd2F5cyA9IF93OwoJfQoJbWluaW1hbEFuZFdheXMgYWRkKGNvbnN0IG1pbmltYWxBbmRXYXlzICZ0aGF0KSBjb25zdAoJewoJCXJldHVybiBtaW5pbWFsQW5kV2F5cyhtaW5pbWFsICsgdGhhdC5taW5pbWFsLCAod2F5cyAqIHRoYXQud2F5cykgJSBtb2QpOwoJfQoJbWluaW1hbEFuZFdheXMgdXBkYXRlKGNvbn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