#include #define F first #define S second #define llong long long #define ullong unsigned long long #define mp make_pair #define pb push_back #define pii pair #define sz(v) (int)v.size() #define pt pair #define x first #define y second #define matrix vector > using namespace std; const int MXN = (int)3e6 + 10; const int INF = (int)1e9 + 7; const llong LINF = (llong)1e18 + 10; const double EPS = (double)1e-6; const double PI = (double)acos(-1.0); int a[MXN]; int n; int pref[MXN]; int f[MXN]; int dp[1222][1222]; int inv[MXN], inv_f[MXN]; inline int binpow(int x, int y){ int ret = 1; while(y){ if(y & 1) ret = (ret * 1LL * x) % INF; x = (x * 1LL * x) % INF; y >>= 1; } return ret; } inline bool check(int x, int y){ return pref[y] - pref[x] == (y - x); } inline int A(int n, int k){ return (f[n] * 1LL * binpow(f[n - k], INF - 2)) % INF; } inline int C(int n, int k){ return (f[n] * 1LL * binpow((f[n - k] * 1LL * f[k]) % INF, INF - 2)) % INF; } int main(){ #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif f[0] = 1; for(int i = 1; i < MXN; ++i){ f[i] = (f[i - 1] * 1LL * i) % INF; } for (int i = 0; i < MXN; ++i){ inv_f[i] = binpow(f[i], INF - 2); } scanf("%d", &n); for(int i = 1; i <= n; ++i){ scanf("%d", &a[i]); pref[i] = pref[i - 1] + (a[i] > a[i - 1]); } for(int i = 1; i <= n; ++i){ if(check(1, i)){ dp[i][i] = 1; } } for(int i = 1; i <= n; ++i){ for(int j = 1; j + j <= i; ++j){ for(int k = j; k + j <= i; ++k){ if(!dp[i - j][k] || !check(i - j + 1, i)){ continue; } //dp[i][j] += (((C(k, j) * 1LL * dp[i - j][k]) % INF) * 1LL * binpow(f[k - j], INF - 2)) % INF; //dp[i][j] += ((C(k, j) * 1LL * dp[i - j][k]) % INF); //dp[i][j] += (((dp[i - j][k] * 1LL * f[j]) % INF) * 1LL * binpow(f[k - j], INF - 2)) % INF; dp[i][j] += (((dp[i - j][k] * 1LL * f[k]) % INF) * 1LL * inv_f[k - j]) % INF; if(dp[i][j] >= INF) dp[i][j] -= INF; } } } /*for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j){ cout << dp[i][j] << " "; } cout << "\n"; }*/ int ans = 0; for(int i = 1; i <= n; ++i){ //ans += (dp[n][i] * 1LL * f[i]) % INF; //ans += (dp[n][i] * 1LL * binpow(f[i], INF - 2)) % INF; //if(check(1, i)) ans += (dp[n][i]); if(ans >= INF) ans -= INF; } /*if(check(1, n)){ ans -= f[n]; ans++; ans %= INF; if(ans < 0) ans += INF; }*/ printf("%d", ans); return 0; }