#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define pb push_back #define pp pop_back #define f first #define s second #define mp make_pair #define sz(a) (int)((a).size()) #ifdef _WIN32 # define I64 "%I64d" #else # define I64 "%lld" #endif #define fname "." typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair < int, int > pi; const int MAX_N = 1223; const int mod = (int)1e9 + 7; int n; int a[MAX_N]; int dp[MAX_N][MAX_N]; int c[MAX_N][MAX_N]; void add(int &a, const int &b) { a += b; if (a >= mod) a -= mod; } int f[MAX_N]; map < vector < vector < int > >, bool > mem; int p[MAX_N]; vector < vector < int > > y, fuckya; vector < int > u, fuck; map < vector < int >, int > ans; void do_it() { sort(y.begin(), y.end()); if (mem[y]) return; mem[y] = 1; fuckya = y; int len = 0; for (auto &i : y) { len = max(sz(i), len); reverse(i.begin(), i.end()); } vector < int > res; for (int i = 0; i < len; i++) { vector < int > shit; for (auto &j : y) if (!j.empty()) { shit.pb(j.back()); j.pp(); } sort(shit.begin(), shit.end()); for (auto j : shit) res.pb(j); } if (res == fuck) { for (auto i : fuckya) { for (auto j : i) printf("%d ", j); printf("\n"); } printf("\n"); } ans[res]++; } void slow() { for (int i = 1; i <= n; i++) p[i] = i; do { for (int i = 0; i < (1 << (n - 1)); i++) { int mask = (i | (1 << (n - 1))); y.clear(); for (int j = 0; j < n; ) { u.clear(); while(1) { u.pb(p[j + 1]); if (mask & (1 << j)) break; j++; } y.pb(u); j++; } do_it(); } } while(next_permutation(p + 1, p + n + 1)); } int main() { #ifdef DEBUG freopen("input.txt", "r", stdin); #endif scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); /* for (int i = 1; i <= n; i++) fuck.pb(a[i]); slow(); cout << ans[fuck] << endl;*/ for (int i = 0; i <= n; i++) { c[i][0] = c[i][i] = 1; for (int j = 1; j < i; j++) { c[i][j] = c[i - 1][j]; add(c[i][j], c[i - 1][j - 1]); } } f[0] = 1; for (int i = 1; i <= n; i++) f[i] = 1ll * f[i - 1] * i % mod; /* for (int i = 1; i <= n; i++) a[i] = i; do { vector < int > fuck; for (int i = 1; i <= n; i++) fuck.pb(a[i]); memset(dp, 0, sizeof dp);*/ for (int i = 1; i <= n; i++) { for (int j = i; j > 0; j--) { if (j + 1 <= i && a[j] > a[j + 1]) break; if (j == 1) { dp[i][i] = 1; break; } for (int k = i - j + 1; k <= j - 1; k++) add(dp[i][i - j + 1], 1ll * dp[j - 1][k] * c[k][i - j + 1] % mod * 1ll * f[i - j + 1] % mod); } /* for (int j = 1; j <= i; j++) if (dp[i][j]) { cout << i << " and " << j << " is " << dp[i][j] << endl; }*/ } int ans = 0; for (int i = 1; i <= n; i++) add(ans, dp[n][i]); printf("%d\n", ans); /* if (ans != ::ans[fuck]) { cout << "shit\n"; for (auto i : fuck) cout << i << ' '; cout << endl; cout << ans << ' '<< ::ans[fuck] << endl; return 0; } } while(next_permutation(a + 1, a + n + 1)); */ return 0; }