#include using namespace std; #define read() freopen("input.txt", "r", stdin) #define write() freopen("output.txt", "w", stdout) #define ff(i,a,b) for(int i = (a); i <= (b); i++) #define fr(i,a,b) for(int i = (a); i >= (b); i--) #define REP( i , n ) for( int i = 0 ; i < n ; i++ ) #define REPI( n , i ) for( int i = n ; i >= 0 ; i-- ) #define sc( x ) scanf( "%d" , &x ) #define sc2( x , y ) scanf( "%d%d" , &x , &y ) #define scd( x ) scanf( "%.9f" , &x ) #define scl( x ) scanf( "%I64d" , &x ) #define pf( x ) printf( "%d\n" , x ) #define pfd( x ) printf( "%.9f\n" , x ) #define pfl( x ) printf( "%I64d\n" , x ) #define rrc( x ) return cout << x , 0 ; #define all( v ) v.begin(),v.end() #define all_r( v ) v.rbegin() , v.rend() #define fi first #define se second #define endl '\n' #define SZ(a) int(a.size()) #define pb push_back #define cln( x , v ) memset( x , v , sizeof( x ) ) #define pi acos(-1.0) #define e2( x ) ( 1LL*( x )*( x ) ) #define r2( x ) sqrt( 1.0*( x ) ) #define ones(x) __builtin_popcount(x) #define MCM( a , b ) ( ( a*b )/( __gcd( a , b ) ) ) #define ddd cout << "despues" << endl ; #define sss cout << "------------------" << endl ; #define aaa cout << "antes" << endl ; #define da( a , b ) ( (a)/(b) - ( (a) < 0 && (a)%(b) != 0 ) ) #define ceil_( a , b ) ( da( (a) , (b) ) + ((a)%(b) > 0) ) #define Mm greater #define nxtPerm next_permutation #define L( n ) 2 * n #define R( n ) 2 * n + 1 #define A 1 , 0 , n - 1 #define endl '\n' typedef double db ; typedef long double ld ; typedef long long ll ; typedef vector vi ; typedef vector vvi ; typedef vector vl ; typedef vector vb ; typedef pair pii ; typedef vector vpii ; const ld EPS = 1e-8 ; const int INF = (int)( INT_MAX - 100 ) ; const ll mod = (int)( 1e+9 + 7 ) ; const int N = (int)( 1200 ) ; int days_month[ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; //inline ll modulo( ll num ){ ( ( num %= mod ) += mod ) %= mod ; return num ; } //inline ll pot( int b , int e ){ ll p = 1 ; REP( i , e ) p = (p*b)%mod ; return p ; } ll C[ N + 2 ][ N + 2 ] ; ll F[ N + 2 ] ; int lim[ N + 2 ] ; int num[ N + 2 ] ; int nxtL[ N + 2 ] ; int n ; void init() { for( int i = 0 ; i <= N ; i ++ ) { for( int j = 0 ; j <= i ; j ++ ){ if( i == j || j == 0 ) C[ i ][ j ] = 1 ; else C[ i ][ j ] = ( C[ i - 1 ][ j - 1 ] + C[ i - 1 ][ j ] ) % mod ; } } F[ 0 ] = 1 ; for( int i = 1 ; i <= N ; i ++ ) { F[ i ] = ( F[ i - 1 ]*i )%mod ; } for( int i = n ; i >= 1 ; i -- ) { nxtL[ i ] = 1 ; if( lim[ i ] == lim[ i + 1 ] ) { nxtL[ i ] += nxtL[ i + 1 ] ; } } } bool used[ N + 2 ][ N + 2 ] ; int memo[ N + 2 ][ N + 2 ] ; int dp( int p , int last ) { if( p > n ) return 1 ; if( used[ p ][ last ] ) return memo[ p ][ last ] ; used[ p ][ last ] = true ; int &ans = memo[ p ][ last ] = 0 ; int max_len = min( last , nxtL[ p ] ) ; for( int len = max_len ; len >= 1 ; len -- ) { ll perm = ( F[ len ]*C[ last ][ len ] ) % mod ; if( p == 1 ) perm = 1 ; ans += ( perm*dp( p + len , len ) ) % mod ; if( ans >= mod ) ans -= mod ; } return ans ; } int main() { // ios_base::sync_with_stdio(0); scanf( "%d" , &n ) ; for( int i = 1 ; i <= n ; i ++ ) { scanf( "%d" , &num[ i ] ) ; } for( int i = 1 ; i <= n ; i ++ ) { lim[ i ] = lim[ i - 1 ] ; if( num[ i - 1 ] > num[ i ] ) { lim[ i ] ++ ; } } init() ; printf( "%d" , dp( 1 , n ) ) ; return 0 ; }