#include #include using namespace std; const long long base = 1e9 + 7; long long mul(long long a, long long b) { return a * b % base; } void add(long long &a, long long diff) { a += diff; if (a >= base) a -= base; if (a < 0) a += base; } const int maxn = 1202; long long f[maxn][maxn]; long long sum[maxn][maxn]; long long prod[maxn][maxn]; int main() { int n = 0; cin >> n; vector < int > v(n + 1, 0); for (int i = 0; i < n; i++) cin >> v[i]; v[n] = n + 1; f[n][0] = 1; for (int i = 0; i <= n; i++) sum[n][i] = 1; vector < int > a(n + 1, 0); for (int i = n - 1; i >= 0; i--) { a[i] = a[i + 1] + 1; if (v[i] > v[i + 1]) a[i] = 1; } for (int i = 0; i <= n; i++) { prod[i][i] = max(i, 1); for (int j = i + 1; j <= n; j++) prod[i][j] = mul(prod[i][j - 1], j); } for (int i = n - 1; i >= 0; i--) { for (int k = 1; k <= a[i]; k++) { if (i + k == n) add(f[i][k], 1); for (int j = 1; j <= k; j++) { if (i + k < n) add(f[i][k], mul(prod[k - j + 1][k], f[i + k][j])); } } } long long sum = 0; for (int i = 0; i <= n; i++) add(sum, f[0][i]); cout << sum << endl; return 0; }