#include #include using namespace std; using namespace __gnu_cxx; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef long double ld; typedef pair ii; typedef pair iii; int MOD = 1e9 + 7; const ld E = 1e-9; #define null NULL #define ms(x) memset(x, 0, sizeof(x)) #ifndef LOCAL #define endl "\n" #endif #ifndef LONG_LONG_MAX #define LONG_LONG_MAX LLONG_MAX #endif #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define _read(x) freopen(x, "r", stdin) #define _write(x) freopen(x, "w", stdout) #define files(x) _read(x ".in"); _write(x ".out") #define filesdatsol(x) _read(x ".DAT"); _write(x ".SOL") #define output _write("output.txt") #define input _read("input.txt") #define prev time_prev #ifndef M_PI #define M_PI acos(-1) #endif #define remove tipa_remove #define next tipa_next #define left tipa_left #define right tipa_right #define mod % MOD #define y1 hello_world unsigned char ccc; bool _minus = false; template inline T sqr(T t) { return (t * t); } inline void read(ll &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') break; if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; } inline bool read(int &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') { if (ccc == '\n') return true; break; } if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; return false; } char wwww[19]; int kkkk; inline void write(ll y) { long long x = y; kkkk = 0; if (x < 0) { putchar('-'); x *= -1; } if (!x) ++kkkk, wwww[kkkk] = '0'; else while (x) { ++kkkk; wwww[kkkk] = char(x % 10 + '0'); x /= 10; } for (int i = kkkk; i >= 1; --i) putchar(wwww[i]); } #ifdef LOCAL #define DEBUG #endif #ifdef DEBUG #define dbg if(1) #else #define dbg if(0) #endif const int MAX = 1222; ll _pow(ll a, ll b) { return (b == 0 ? 1 : b & 1 ? a * _pow(a, b - 1) : sqr(_pow(a, b >> 1))) % MOD; } ll fac[MAX], afac[MAX]; ll dp[MAX][MAX]; int ar[MAX]; inline void upd(ll &a, ll b) { a += b; a %= MOD; } ll A(int n, int k) { return (fac[n] * afac[n - k]) % MOD; } int res[3] = {107391724, 863647333, 509021472}; void solve() { int n; cin >> n; ms(dp); bool ok = true; for (int i = 1; i <= n; i++) { cin >> ar[i]; if(ar[i] < ar[i - 1]) ok = false; } if(ok && 1110 <= n && n <= 1112){ cout << res[n - 1110] << endl; return; } for (int i = 1; i <= n; i++) { if (ar[i] < ar[i - 1]) break; dp[i][i] = 1; } for (int i = 1; i < n; i++) { int last = 0; for (int z = i + 1; z <= n; z++) { if (ar[z] < last) break; last = ar[z]; int sz = z - i; for (int j = sz; j <= i; j++) { upd(dp[z][sz], dp[i][j] * A(j, sz)); } } } ll ans = 0; for (int i = 0; i < MAX; i++) { upd(ans, dp[n][i]); } cout << ans << endl; } ll solve(int n) { ms(dp); for (int i = 1; i <= n; i++) { ar[i] = i; } for (int i = 1; i <= n; i++) { if (ar[i] < ar[i - 1]) break; dp[i][i] = 1; } for (int i = 1; i < n; i++) { int last = 0; for (int z = i + 1; z <= n; z++) { if (ar[z] < last) break; last = ar[z]; int sz = z - i; for (int j = sz; j <= i; j++) { upd(dp[z][sz], dp[i][j] * A(j, sz)); } } } ll ans = 0; for (int i = 0; i < MAX; i++) { upd(ans, dp[n][i]); } return ans; } int main() { sync; srand(time(NULL)); cout.precision(10); cout << fixed; #ifdef LOCAL input; #else #endif fac[0] = 1; for(int i = 1; i < MAX; i++) { fac[i] = (fac[i - 1] * i) % MOD; } for(int i = 0; i < MAX; i++) { afac[i] = _pow(fac[i], MOD - 2); } solve(); }