/*input 3 1 2 */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define sp ' ' #define endl '\n' #define fi first #define se second #define mp make_pair #define int long long #define N 1205 // const int INF = 1e18; const int mod = 1e9 + 7; int n; int a[N]; int lis[N]; int dp[N][N]; int c[N][N]; int expo[N]; void prep() { lis[n] = 1; for (int i = n - 1; i >= 1; i--) { if (a[i] <= a[i + 1]) lis[i] = lis[i + 1] + 1; else lis[i] = 1; } for (int i = 1; i <= N - 5; i++) c[0][i] = 1, c[1][i] = i; for (int i = 2; i <= N - 5; i++) { for (int j = i; j <= N - 5; j++) { c[i][j] = (c[i][j - 1] + c[i - 1][j - 1]) % mod; } } expo[1] = 1; for (int i = 2; i <= N - 5; i++) expo[i] = (expo[i - 1] * i) % mod; memset(dp, -1, sizeof(dp)); } int C(int k, int n) { return c[k][n]; } int cal(int p, int lim) { if (p == n + 1) return 1; int &ret = dp[p][lim]; if (ret != -1) return ret; ret = 0; for (int i = 1; i <= min(lis[p], lim); i++) { ret = (ret + (cal(p + i, i) * C(i, lim)) % mod * expo[i]) % mod; } return ret; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; // optimize later for (int i = 1; i <= n; i++) cin >> a[i]; prep(); int ans = 0; for (int i = 1; i <= lis[1]; i++) { ans = (ans + cal(i + 1, i)) % mod; } cout << ans << endl; }