#include using namespace std; #define int long long int #define MOD 1000000007 #define MAX 1300 int val[MAX]; int dp[MAX][MAX]; // dp[i][k] = up to i-th charater, last having k nonempty int fact[MAX]; int comb[MAX][MAX]; typedef int lli; // returns a^x mod p int exponent_(lli a, lli x, lli p) { lli ans = 1; while (x > 0) { if (x % 2 == 1) ans = (ans * a) % p; x /= 2; a = (a * a) % p; } return ans; } int modular_inverse_(lli a, lli b, lli p) { return ((a % p) * (exponent_(b, p - 2, p) % p)) % p; } int combinatorics_(lli n, lli r, lli p) { if (comb[n][r] != -1) return comb[n][r]; return comb[n][r] = (modular_inverse_(fact[n], (fact[r] * fact[n - r]) % p, p)) % p; } #undef int int main() { #define int long long int memset(comb, -1, sizeof(comb)); fact[0] = 1; for (int i = 1; i < MAX; i++) fact[i] = (fact[i - 1] * i) % MOD; int N; cin >> N; for (int i = 1; i <= N; i++) { cin >> val[i]; } for (int i = N; i >= 1; i--) { if (i < N && val[i] > val[i + 1]) break; dp[i][N - i] = 1; } int ans = 0; for (int i = N - 1; i >= 1; i--) { for (int k = 0; i - k >= 1; k++) { if (k > 0 && val[i - k] > val[i - k + 1]) break; for (int j = 0; j <= k && i + 1 + j <= N; j++) { dp[i - k][k] = (dp[i - k][k] + (((combinatorics_(k + 1, j + 1, MOD) * fact[j + 1]) % MOD) * dp[i + 1][j]) % MOD) % MOD; } } } for (int i = 0; i < N; i++) { ans = (ans + dp[1][i]) % MOD; } cout << (ans + MOD) % MOD << "\n"; return 0; }