#include #include #include #include #include #include #include #include #include #include #include #include #define MAXX 1300 #define pb push_back #define all(x) x.begin(), x.end() #define CLR(x) memset(x, 0, sizeof(x)) #define INF 1000000000 #define ll long long #define MOD 1000000007LL using namespace std; ll facts[MAXX]; ll C[MAXX][MAXX]; ll perm[MAXX][MAXX]; ll dp[MAXX][MAXX], m[MAXX], N; bool vis[MAXX][MAXX]; int dips[MAXX]; void make_facts(ll n){ facts[0] = 1LL; for(ll i = 1LL; i <= n; i++){ facts[i] = (facts[i-1] * i)%MOD; } return; } void make_perm(ll n){ int i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= i; j++) { // Base Cases if (j == 0 || j == i){ C[i][j] = 1LL; } // Calculate value using previosly stored values else{ C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD; } perm[i][j] = (C[i][j] * facts[j])%MOD; } } } ll call(int pos, int grps){ if((long long)pos == N && grps == 0) return 1LL; if((long long)pos >= N && grps > 0) return 0LL; if(pos > N) return 0LL; if(grps == 0) return 0LL; if(vis[pos][grps]) return dp[pos][grps]; ll ways = 0LL; if(pos < N && dips[pos+grps-1] == dips[pos]){ for(int l = 0; l <= grps; l++){ ways = (ways + (call(pos+grps, l) * perm[grps][l])%MOD) % MOD; } } vis[pos][grps] = true; return dp[pos][grps] = ways; } ll call2(int pos, int grps){ if((long long)pos == N) return 1; if(pos > N) return 0; if(grps == 0) return 0; if(vis[pos][grps]) return dp[pos][grps]; ll ways = 0LL; if(pos < N && dips[pos+grps-1] == dips[pos]){ if (pos > 0) ways = ((facts[grps] * call(pos+grps, grps))%MOD + (call(pos, grps-1))%MOD)%MOD; else ways = ((call(pos+grps, grps))%MOD + (call(pos, grps-1))%MOD)%MOD; }else{ if(pos > 0) ways = (call(pos, grps-1))%MOD; else ways = call(pos, grps-1); } vis[pos][grps] = true; return dp[pos][grps] = ways; } int main(){ int i, j; scanf("%lld", &N); make_facts(N+2); make_perm(N+2); memset(dips, -1, sizeof(dips)); dips[0] = 0; for(i = 0; i < N; i++){ scanf("%lld", &m[i]); dips[i] = dips[i-1]; if(i > 0 && m[i] < m[i-1]) dips[i]++; } CLR(vis); ll ans = 0LL; for(int i = 0; i <= N; i++){ ans = (ans + call(0, i))%MOD; } printf("%lld\n", ans); /*for(i = 0; i <= N; i++){ for(j = 0; j <= N; j++){ printf("%lld ", perm[i][j]); } printf("\n"); }*/ return 0; }