#include "bits/stdc++.h" using namespace std; const int N = 1212; const int MOD = 1000000007; int n; int arr[N], next_inv[N], dp[N][N]; int fact[N], inv_fact[N], choose[N][N]; inline int add(int x, int y) { int res = x + y; if (res >= MOD) { res -= MOD; } return res; } inline int prod(int x, int y) { long long res = x * 1LL * y; if (res >= MOD) { res %= MOD; } return res; } inline int power(int x, int y) { if (y == 0) { return 1; } int res = power(x, y >> 1); res = prod(res, res); if (y & 1) { res = prod(res, x); } return res; } inline void preprocess() { fact[0] = inv_fact[0] = 1; for (int i = 1; i < N; i++) { fact[i] = prod(fact[i - 1], i); inv_fact[i] = power(fact[i], MOD - 2); } choose[0][0] = 1; for (int a = 1; a < N; a++) { choose[a][0] = choose[a][a] = 1; for (int b = 1; b < a; b++) { choose[a][b] = add(choose[a - 1][b], choose[a - 1][b - 1]); } } } int main() { preprocess(); cin >> n; for (int i = 1; i <= n; i++) { cin >> arr[i]; } next_inv[n] = n; for (int i = n - 1; i >= 1; i--) { if (arr[i] > arr[i + 1]) { next_inv[i] = i; } else { next_inv[i] = next_inv[i + 1]; } } dp[n + 1][0] = 1; for (int i = n; i >= 1; i--) { for (int j = 1; j <= n; j++) { int allowed = next_inv[i] - i + 1; if (allowed < j) { continue; } dp[i][j] = fact[j]; int res = 0; for (int k = 0; k <= j; k++) { res = add(res, prod(choose[j][j - k], dp[i + j][k])); } dp[i][j] = prod(dp[i][j], res); } } int res = 0; for (int i = 1; i <= n; i++) { res = add(res, prod(dp[1][i], inv_fact[i])); } cout << res << "\n"; }