#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define sz(a) (int)(a.size()) #define int long long int p[1210][1210]; int n; int b[1210]; int a[1210]; const int MOD = 1e9 + 7; int dp[1210][1210]; int C[1210][1210]; int F[1210]; signed main() { ios_base::sync_with_stdio(0); cin >> n; int m = 0; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 2; i <= n; ++i) { if (a[i] < a[i - 1]) { b[i] = 1; } } F[0] = 1; for (int i = 1; i <= n; ++i) { F[i] = F[i - 1] * i % MOD; C[i][0] = 1; C[i][i] = 1; for (int j = 1; j < i; ++j) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; } int ans = 0; for (int i = 1; i <= n; ++i) { if (b[i]) break; dp[i][i] = 1; } for (int i = 2; i <= n; ++i) { for (int j = 1; j <= i; ++j) { int ok = 1; for (int k = i; k > i - j + 1; --k) if (b[k]) ok = 0; if (!ok) break; for (int k = j; k <= i; ++k) dp[i][j] = (dp[i][j] + (dp[i - j][k] * C[k][j]) % MOD * F[j]) % MOD; } } for (int i = 1; i <= n; ++i) ans = (ans + dp[n][i]) % MOD; cout << ans; return 0; }