#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector vll; typedef pair pll; typedef pair pii; typedef vector vii; typedef complex Point; #define csl ios_base::sync_with_stdio(false); cin.tie(NULL) #define mp make_pair #define fst first #define snd second long long t, n, m, u, v, q, k; const int N = 1205; const long long mod = 1e9 + 7; const long long INF = 1LL << 61LL; long long arr[N], brr[N]; string str, ss; long long dp[N][N]; int R[N]; long long Cnk[N][N]; ll fact[N], ifact[N], inv[N]; void _pre() { fact[1] = fact[0] = 1; ifact[0] = ifact[1] = 1; inv[0] = inv[1] = 1; for (int i = 1; i < N; ++i) fact[i] = (fact[i - 1] * i) % mod; for (int i = 2; i < N; ++i) inv[i] = (((-(mod / i) * inv[mod % i]) % mod) + mod) % mod; for (int i = 2; i < N; ++i) ifact[i] = (ifact[i - 1] * inv[i]) % mod; } ll C(int n, int k) { if (k > n) return 0; if (n < 0 || k < 0) return 0; return (fact[n] * ((ifact[n - k] * ifact[k]) % mod)) % mod; } int main() { csl; cin >> n; for (int i = 1; i <= n; ++i) { cin >> arr[i]; } R[n] = n; _pre(); for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) Cnk[i][j] = C(i, j); } for (int i = n - 1; i >= 1; --i) { if (arr[i + 1] >= arr[i]) R[i] = R[i + 1]; else R[i] = i; } for (int i = 1; i <= R[1]; ++i) { dp[i][i] = 1; } for (int i = 1; i <= n - 1; ++i) { for (int j = 1; j <= i; ++j) { if (dp[i][j] == 0) continue; //cout << i << " " << j << dp[i][j] << '\n'; for (int k = 1; k <= j; ++k) { if (R[i + 1] - (i + 1) + 1 < k) break; dp[i + 1 + (k - 1)][k] = (dp[i + 1 + (k - 1)][k] + fact[k] * (1LL * (Cnk[j][k] * dp[i][j])% mod)) % mod; } } } int sol = 0; for (int i = 1; i <= n; ++i) { sol += dp[n][i]; if (sol >= mod) sol -= mod; } cout << sol << '\n'; return 0; }