#include #include #include #include #define MOD 1000000007 using namespace std; typedef long long ll; int a[1500]; ll dp[1505][1505]; int N; ll f[1505][1505]; int d[1505][1505]; bool dec(int n, int m){ if(d[n][m] != -1) return d[n][m]; if(m == 1) return true; int ans = dec(n,m-1) and ( a[n+m-2] < a[n+m-1]); d[n][m] = ans; return ans; } void precompute(){ for(ll s = 0; s <= N; ++s){ f[s][s] = 1; for(ll x = s+1; x <= N; ++x) f[x][s] = (x*f[x-1][s]+MOD) % MOD; } for(ll s = 0; s <= N; ++s) for(ll x = s+1; x <=N; ++x) if(f[x][s] < 0) cout << f[x][s] << endl; } ll fact(ll x, ll s){ //cout << "FACT: " << x << " " << s << " " << f[x][s] << endl; return (f[x][s] + MOD)%MOD; } ll solve(int n, int m){ if(n + m > N) return 0; if(!dec(n,m)) return 0; if(n+m == N) return 1; if(dp[n][m] != -1) return dp[n][m]; ll ans = 0; for(int j = 1; j <= m; ++j) ans = (ans + 1ll*fact(m,m-j)*solve(n+m,j)+MOD) % MOD; return dp[n][m] = ans % MOD; } int main(){ memset(dp,-1,sizeof(dp)); memset(d,-1,sizeof(d)); cin >> N; precompute(); for(int i = 0; i < N; ++i)cin >> a[i]; //for(int i = 0; i < N; ++i) cout << a[i] << " "; cout << endl; ll ans = 0; for(int i = 1; i <= N; ++i){ //cout << i << ". " << solve(0,i) % MOD << endl;; ans = (ans + solve(0,i) + MOD) % MOD; if(ans < 0) ans+=MOD; } cout << (ans + MOD)% MOD << endl; }