#include using namespace std; #define TRACE(x) x #define WATCH(x) TRACE(cout << #x" = " << x << endl) #define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl) #define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());}) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef vector vb; typedef vector vi; typedef vector vvi; typedef vector vll; typedef vector vvll; const int MOD = 1e9 + 7; int sum(int a, int b) { return (a + b) % MOD; } int prod(int a, int b) { return a * 1ll * b % MOD; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); const int MAXN = 1500; vi fact(MAXN); fact[0] = 1; for (int i = 1; i < MAXN; i++) fact[i] = prod(i, fact[i-1]); vvi ncr(MAXN, vi(MAXN + 1)); ncr[0][0] = 1; for (int n = 1; n < MAXN; n++) { ncr[n][0] = 1; for (int r = 0; r <= n; r++) ncr[n][r] = sum(ncr[n-1][r], ncr[n-1][r-1]); } int N; cin >> N; vi vals(N); for (int i = 0; i < N; i++) cin >> vals[i]; vvi ways(N + 1, vi(N + 1, 0)); // ways[i][j] = # of ways to arrange vals[i:] into V of size j //vvi sums(N + 1, vi(N + 1, 0)); // sums[i][j] = # of ways to arrange vals[i:] into V of size <= j ways[N][0] = 1; //fill(all(sums[N]), 1); for (int i = N - 1; i >= 0; i--) { for (int j = 1; j <= N; j++) { if (i + j > N) break; if (j > 1 && vals[i+j-1] < vals[i+j-2]) break; for (int k = 0; k <= j; k++) { int put = prod(ncr[j][k], fact[k]); ways[i][j] = sum(ways[i][j], prod(put, ways[i+j][k])); } } /*for (int j = 1; j <= N; j++) { sums[i][j] = sum(ways[i][j], sums[i][j-1]); }*/ } int ans = 0; for (int j = 1; j <= N; j++) { ans = sum(ans, ways[0][j]); } cout << ans << endl; return 0; }