#include #include #include #include #include using namespace std; typedef long long ll; const int maxn = 1234; const ll p = 1000000007; ll dp[maxn][maxn]; int ok[maxn][maxn]; int a[maxn]; int psum[maxn]; ll fac[maxn]; ll ifac[maxn]; int n; bool is_sorted(int s, int e) { return psum[e - 1] - psum[s] == e - s - 1; } ll po(ll b, ll e) { if (e == 0) return 1; ll ret = po(b, e / 2); ret = (ret * ret) % p; if (e & 1) ret = (ret * b) % p; return ret; } ll inv(ll x) { return po(x, p - 2); } ll get(int r, int s) { if (dp[r][s] != -1) return dp[r][s]; if (!ok[r][s]) return dp[r][s] = 0; if (s == n) return dp[r][s] = 1; ll ret = 0; int k = s - r; int bound = min(n, s + k); for (int i = s + 1; i <= bound; i++) { ret = (ret + get(s, i) * ifac[k - (i - s)]) % p; } return dp[r][s] = (ret * fac[k]) % p; } int main() { for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) dp[i][j] = -1; cin >> n; cin >> a[0]; psum[0] = 0; for (int i = 1; i < n; i++) { cin >> a[i]; psum[i] = psum[i - 1] + (a[i] > a[i - 1]); } fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; for (int i = 0; i <= n; i++) ifac[i] = inv(fac[i]); for (int i = 0; i <= n; i++) { for (int j = i + 1; j <= n; j++) { ok[i][j] = is_sorted(i, j); } } ll ans = 0; for (int i = 1; i <= n; i++) ans = (ans + get(0, i)) % p; cout << ans << endl; }