#include using namespace std; const int n_ = 1201; typedef long long ll; int n; ll arr[n_]; ll memo[n_][n_]; ll mod = 1e9 + 7; ll comb[n_][n_]; // Initialize it with -1 ll fact [n_]; ll nCr(int n, int k) // O(n * k) { if (n < k) return 0; if (k == 0 || k == n)//may add k == 1 as a base case for fast calculations return 1; if (comb[n][k] != -1) return comb[n][k]; if (n - k < k) return comb[n][k] = nCr(n, n - k); //reduce k to n - k return comb[n][k] = (nCr(n - 1, k - 1) + nCr(n - 1, k)) % mod; } ll fac (int num) { if (fact[num] != -1) return fact[num]; if (num == 0 || num == 1) return fact[num] = 1; return fact[num] = num * fac(num - 1) % mod; } ll dp (int height, int idx) { if (idx == n) return 1; ll &ret = memo[height][idx]; if (ret != -1) return ret; ret = (height * dp(1, idx + 1)) % mod; for (int i = idx + 1; i < n; i++) { if (arr[i] < arr[i - 1] || i - idx + 1 > height) break; ll ansHere = (nCr(height, i - idx + 1) * dp(i - idx + 1, i + 1) % mod) * fac(i - idx + 1) % mod; ret = (ret + ansHere) % mod; } return ret; } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; memset (memo, -1, sizeof memo); memset(comb, -1, sizeof comb); memset(fact, -1, sizeof fact); ll res = dp(1,1); for (int i = 1; i < n; i++) { if (arr[i] < arr[i - 1]) break; else res = (res + dp(i + 1, i + 1)) % mod; } cout << res << "\n"; }