#define _CRT_SECURE_NO_DEPRECATE #define _CRT_SECURE_NO_WARINGS #define _USE_MATH_DEFINES #include using namespace std; typedef long long int64; typedef unsigned long long uint64; typedef pair pii; typedef pair pll; const int INF = (int)(1e9 + 1337); const int64 LINF = (int64)(4e18); const double EPS = (double)(1e-11); #define sq(x) ((x)*(x)) #define FAIL() ((*(int*)(0))++) const int MOD = 1000000007; int add(int a, int b) { int res = a + b; if(res >= MOD) res -= MOD; return res; } int sub(int a, int b) { int res = a - b; if(res < 0) res += MOD; return res; } int mul(int a, int b) { return (int64)a * b % MOD; } int binpow(int a, int p) { int res = 1; while(p) { if(p & 1) res = mul(res, a); a = mul(a, a); p >>= 1; } return res; } int obr(int a) { return binpow(a, MOD - 2); } int n; int a[1210]; int r[1210]; int f[1210][1210]; int fact[100010]; int rfact[100010]; void precalc() { fact[0] = 1; rfact[0] = 1; for(int i = 1; i < 100010; i++) { fact[i] = mul(fact[i - 1], i); rfact[i] = obr(fact[i]); } } bool check(int l, int r) { return r < ::r[l]; } int dyn(int pos, int len) { //if(len == 1) // return 1; if(pos == n) return 1; if(f[pos][len] != -1) return f[pos][len]; int res = 0; if(len != 1) res = add(res, mul(dyn(pos + 1, len - 1), len)); if(check(pos, pos + len - 1)) res = add(res, mul(dyn(pos + len, len), fact[len])); return f[pos][len] = res; } int dyn2(int pos, int len) { if(len == 1) return 1; if(pos == n) return 1; if(f[pos][len] != -1) return f[pos][len]; int res = 0; for(int i = 1; i <= len && check(pos, pos + i - 1); i++) res = add(res, mul(dyn2(pos + i, i), mul(fact[len], rfact[len - i]))); return f[pos][len] = res; } void solve() { cin >> n; for(int i = 0; i < n; i++) { cin >> a[i]; } for(int i = 0; i < n;) { int ctr = 1; while(i + ctr < n && a[i + ctr] > a[i + ctr - 1]) ctr++; for(int j = i; j < i + ctr; j++) r[j] = i + ctr; i += ctr; } memset(f, -1, sizeof f); int ans = 0; for(int i = 1; i <= n && check(0, i - 1); i++) { int tans = dyn2(i, i); //cout << i << ' ' << tans << '\n'; ans = add(ans, tans); } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); precalc(); solve(); }