// HackerRank - University CodeSprint 2 - Sherlock's Array Merging Algorithm #include using namespace std; const int N = 1209; const long long MOD = 1000000007; int n, a[N]={}; long long c[N]={}, inv[N]={}, dp[N][N]={}; bool b[N][N]={}; long long pow_mod(long long a, long long n){ if(n == 1) return a; long long temp = pow_mod(a, n / 2); temp *= temp; temp %= MOD; if(n % 2 == 1){ temp *= a; temp %= MOD; } return temp; } int main(){ scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", &a[i]); c[0] = inv[0] = 1; for(int i=1; i<=n; i++){ c[i] = (c[i - 1] * (long long) i) % MOD; inv[i] = pow_mod(c[i], MOD - 2); } for(int i=1; i<=n; i++) b[i][i] = true; for(int i=1; i a[j - 1]) b[i][j] = b[i][j - 1]; else b[i][j] = false; for(int i=1; i<=n; i++) for(int j=1; j<=i; j++){ if(!b[i-j+1][i]) continue; if(i == j){ dp[i][j] = 1; continue; } for(int k=j; k<=i-j; k++){ dp[i][j] += (((dp[i - j][k] * c[k]) % MOD) * inv[k - j]) % MOD; dp[i][j] %= MOD; } } long long ans = 0; for(int i=1; i<=n; i++){ ans += dp[n][i]; ans %= MOD; } printf("%lld\n", ans); return 0; }