#include using namespace std; using ll =long long; using vl=vector; using vb=vector; using vs=vector; using vvl=vector; using pll=pair; const ll oo =0x3f3f3f3f3f3f3f3fLL; const double eps=1e-9; #define sz(c) ll((c).size()) #define all(c) begin(c),end(c) #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define xx first #define yy second #define FOR(i,a,b) for(ll i=(a); i<(b); i++) #define FORD(i,a,b) for(ll i=ll(b)-1;i>=(a);i--) #define TR(X) ({if(1) cerr << "TR: " << (#X) << " = " << (X) << endl; }) vvl ascending; vl vals; vvl factorials; vvl mem; const ll MOD = 1e9+7; ll doit(ll pos, ll v) { if (pos == sz(vals) && v == 0) return 1; else if (v == 0 || pos >= sz(vals)) return 0; /* // TODO efficient ascending check ll cur = vals[pos]; FOR(i, 1, v) { if (pos+i >= sz(vals) || vals[pos+i] < cur) { return 0; } cur = vals[pos+i]; } */ if (pos+v-1 >= sz(vals) || !ascending[pos][pos+v-1]) { return 0; } if (mem[pos][v] != -1) return mem[pos][v]; ll res = 0; FOR(nv, 0, v+1) { // We use v buckets // And have poss possibilities to produce the rest with nv buckets // v * (v-1) * (v-2) * ... (v-nv+1) // v! / nv! ll poss = doit(pos+v, nv); // TODO efficient and modulo! ll combinations = factorials[v][nv]; /* if (nv != 0) { ll acc = v; FOR(i, 0, nv) { combinations *= acc; acc --; } //FOR(mul, nv+1, v+1) combinations *= mul; } */ res += (poss * combinations) % MOD; res %= MOD; } return mem[pos][v] = (res % MOD); } int main(){ cin.sync_with_stdio(0); ll n; cin >> n; vals.resize(n); FOR(i, 0, n) cin >> vals[i]; ascending.assign(n, vl(n, false)); FOR(start, 0, n) { ll cur = vals[start]; FOR(end, start, n) { if (vals[end] < cur) break; ascending[start][end] = true; cur = vals[end]; } } factorials.assign(n+1, vl(n+1, 1)); FOR(v, 1, n+1) { ll val = 1; ll mul = v; FOR(nv, 0, n+1) { factorials[v][nv] = val % MOD; val *= mul; val %= MOD; mul --; } } mem.assign(n+1, vl(n+1, -1)); /* cerr << "v" << endl; FORD(v, 0, n+1) { cerr << v; FOR(i, 0, n+1) cerr << " " << doit(i, v); cerr << endl; } cerr << "i"; FOR(i, 0, n+1) { cerr << " " << i; } cerr << endl; cerr << "M"; FOR(i, 0, n) { cerr << " " << vals[i]; } cerr << " -" << endl; */ ll res = 0; FOR(v, 0, n+1) res = (res + doit(0, v)) % MOD; cout << res << endl; } //cin.tie(0) bei schnellem Wechseln