#include #include #include #include #include using namespace std; const int BASE = 1e9 + 7; int pw(int x, int y) { int res = 1; while (y) { if (y & 1) res = (res * 1LL * x) % BASE; x = (x * 1LL * x) % BASE; y /= 2; } return res; } int main() { // freopen("input.txt", "r", stdin); int n; scanf("%d", &n); vector a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); vector sz; int cur_sz = 1; for (int i = 0; i + 1 < n; i++) { if (a[i] > a[i + 1]) { sz.push_back(cur_sz); cur_sz = 1; } else ++cur_sz; } vector fact(n + 1); fact[0] = 1; for (int i = 0; i + 1 < (int)fact.size(); i++) fact[i + 1] = (fact[i] * 1LL * (i + 1)) % BASE; vector rev(n + 1); for (int i = 0; i < (int)fact.size(); i++) rev[i] = pw(fact[i], BASE - 2); sz.push_back(cur_sz); auto C = [&](int n, int k) { if (n < k) return 0; int res = (fact[n] * 1LL * rev[k]) % BASE; res = (res * 1LL * rev[n - k]) % BASE; return res; }; vector> f(n + 1, vector(n + 1)); for (int i = 1; i <= sz[0]; i++) f[i][i] = 1; for (int i = 1; i <= n; i++) { for (int x = 1; x <= i; x++) { if (f[i][x] != 0) { for (int j = i; j < min(n, i + x); j++) { if ((j != i) && (a[j] < a[j - 1])) break; int cur = (f[i][x] * 1LL * C(x, j - i + 1)) % BASE; cur = (cur * 1LL * fact[j - i + 1]) % BASE; f[j + 1][j - i + 1] = (f[j + 1][j - i + 1] + cur) % BASE; } } } } int res = 0; for (int i = 1; i <= n; i++) res = (res + f[n][i]) % BASE; printf("%d\n", res); return 0; }