#include using namespace std; const int N = 1222; const long long mod = (long long)1e9 + 7; int n, a[N], b[N]; long long f[N][N], ff[N], rff[N]; long long C[N][N]; long long power(long long a, long long n) { if (n == 0) return 1LL; if (n % 2 == 0LL) { long long x = power(a, n / 2LL); return (x * x) % mod; } else { return (a * power(a, n - 1)) % mod; } } void CNK(){ for (int n = 0; n < N; ++n) { C[n][0] = C[n][n] = 1; for (int k = 1; k < n; ++k) C[n][k] = (1LL * C[n - 1][k - 1] + 1LL * C[n - 1][k]) % mod; } ff[0] = 1LL; for (int i = 1; i < N; ++i) ff[i] = (ff[i - 1] * i) % mod; for (int i = 0; i < N; ++i) rff[i] = power(ff[i], mod - 2); } int solve(int cntV, int cntN) { if (cntV < 0) return 0; if (cntN < 0) return 0; if (cntN == 0 && cntV >= 0) return 1; long long &res = f[cntV][cntN]; if (res != -1) return res; res = 0; int p = n - cntN; for (int i = p; i - p + 1 <= cntV && i < n; ++i) { if (i == p) { (res += 1LL * cntV * solve(1, cntN - 1)) %= mod; } else { if (a[i - 1] > a[i]) break; (res += 1LL * ((C[cntV][i - p + 1] * ff[i - p + 1]) % mod) * solve(i - p + 1, cntN - (i - p + 1))) %= mod; } } return res; } int main() { ios_base::sync_with_stdio(false); // ifstream cin("input.txt"); cin >> n; CNK(); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) f[i][j] = -1; long long ans = 0LL; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { if (i == 1 || (a[i - 2] <= a[i - 1])) (ans += solve(i, n - i)) %= mod; if (a[i - 2] > a[i - 1]) break; } cout << ans << endl; return 0; }