// author: gary #include using namespace std; typedef long long ll; typedef pair pii; #define ALL(x) (x).begin(), x.end() const int inf = 1e9; const int maxn = 1500; const int mod = 1e9 + 7; void madd(int& a, int b) { a += b; if(a >= mod) a -= mod; } int mul(int a, int b) { return (1LL * a * b) % mod; } int mpow(int a, int n) { int r = 1; while(n > 0) { if(n & 1) r = mul(r, a); a = mul(a, a); n >>= 1; } return r; } int n; int a[maxn]; int s[maxn]; int f[maxn], fi[maxn]; int dp[maxn][maxn]; int sorted(int i, int j) { return j - i == s[j-1]-s[i-1]; } int ncr(int n, int k) { return mul(f[n], mul(fi[n-k],fi[k])); } int rec(int i, int j) { if(i == n + 1) return 1; int& ans = dp[i][j]; if(ans != -1) return ans; ans = 0; for(int k = 0; k < j; k++) if(i + j - k - 1 <= n && sorted(i, i + j - k - 1)) madd(ans, mul(rec(i + j - k, j - k), mul(f[j], fi[k]))); return ans; } int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", a + i); for(int i = 1; i < n; i++) { s[i] = a[i] < a[i + 1]; s[i] += s[i-1]; } f[0] = fi[0] = 1; for(int i = 1; i < maxn; i++) { f[i] = mul(f[i-1], i); fi[i] = mul(fi[i-1], mpow(i, mod-2)); } memset(dp, -1, sizeof dp); int res = 0; for(int k = 1; k <= n && sorted(1, k); k++) { madd(res, rec(1+k, k)); } printf("%d\n", res); return 0; }