#define _CRT_SECURE_NO_WARNINGS #include using namespace std; using LL = long long; using vi = vector; using pii = pair; using vpii = vector>; using vll = vector; using LD = long double; #define ALL(v) v.begin(), v.end() #define endl '\n' #define SYNC ios_base::sync_with_stdio(false); cin.tie(NULL); cerr.tie(NULL); #define REP(i , n) for(int i =0 ; i < n ;i++) #define REPN(i , n) for(int i = 1; i <= n ; i++) #define cot(x) cerr << #x << " " << x << " "; #define cotd(x) cerr << #x << " " << x << endl; #define her cerr << " here \n"; #define pp push_back #define fi first #define se second #define un(x) x.erase(unique(ALL(x)), x.end()) template void check(T & a, const T & b) { if (a >= b) { a %= b; } } templateT gcd(T u, T v) { if (u == v)return u; if (u == 0)return v; if (v == 0)return u; if (~u & 1) { if (v & 1) return gcd(u >> 1, v); else return gcd(u >> 1, v >> 1) << 1; }if (~v & 1)return gcd(u, v >> 1); if (u > v)return gcd((u - v) >> 1, v); return gcd((v - u) >> 1, u); } LL mulmod(LL a, LL b, const LL & m) { LL q = (LL)(((LD)a*(LD)b) / (LD)m); LL r = a*b - q*m; if (r>m)r %= m; if (r<0)r += m; return r; } template T expo(T e, S n) { T x = 1, p = e; while (n) { if (n & 1)x = x*p; p = p*p; n >>= 1; }return x; } template T powerL(T e, T n, const T & m) { T x = 1, p = e; while (n) { if (n & 1)x = mulmod(x, p, m); p = mulmod(p, p, m); n >>= 1; }return x; } const int N = 1210; const LL MOD = (int)1e9 + 7; int a[N]; vector DP[N]; void pre(){ DP[0].assign(N,1); for(int i = 1; i <= N; i++){ DP[i].resize(N); DP[i][0] = 1; for(int j = 1; j <= N; j++){ DP[i][j] = (i * DP[i-1][j-1]); check(DP[i][j], MOD); } } } int main(){ SYNC; pre(); int n; cin >> n; REP(i , n){ cin >> a[i]; } vector dp[2]; dp[1].assign(N, 0); dp[1][0] = 1; for(int i = n - 1; i >= 0;){ int cnt = 0, num = (int)1e9; bool here = false; dp[0] = dp[1]; dp[1].assign(N, 0); while(i >= 0 && a[i] < num){ num = a[i]; i--; cnt++; } for(int j = 0; j < cnt; j++){ LL temp = 0; if(!j){ for(int x = 0; x <= cnt; x++){ if(!dp[0][x]) continue; //cerr << dp[0][x] << endl; temp = (temp + mulmod(DP[cnt][x], dp[0][x], MOD)); check(temp,MOD); } dp[1][cnt] = temp; }else{ int g = cnt - j; for(int x = 0; x <= j; x++){ if(!dp[0][x]) continue; temp = (temp + mulmod(DP[j][x], dp[0][x], MOD)); check(temp, MOD); } dp[1][j] = mulmod(temp, DP[g][j], MOD); } } } LL ans = 0; REPN(i , N){ ans = (ans + dp[1][i]); check(ans, MOD); } cout << ans << endl; return 0; }