#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include //#include //#include #include //#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define forn(i, n) for(int i = 0; i < int(n); i++) #define forn1(i, n) for(int i = 1; i <= int(n); i++) #define sz(a) int((a).size()) #define all(a) (a).begin(), (a).end() #define mp make_pair #define pb push_back #define x first #define y second typedef long long li; typedef long double ld; typedef pair pt; const int INF = int(1e9); const li INF64 = li(1e18); const ld PI = acosl(ld(-1)); const ld EPS = 1e-9; template inline T sqr(const T& x) { return x * x; } template inline T abs(const T& x) { return x > 0 ? x : -x; } inline bool inside(int x, int y, int n, int m) { return x >= 1 && x <= n && y >= 1 && y <= m; } inline int rnd() { return abs(rand() ^ (rand() << 15)); } inline int rnd(int n) { assert(n > 0); return rnd() % n; } inline int rnd(int lf, int rg) { return lf + rnd(rg - lf + 1); } inline li rndLL() { return rnd() * 1LL * rnd() + rnd(); } const int dx[4] = { -1, 0, +1, 0 }; const int dy[4] = { 0, +1, 0, -1 }; const int dx8[8] = { -1, -1, 0, +1, +1, +1, 0, -1 }; const int dy8[8] = { 0, +1, +1, +1, 0, -1, -1, -1 }; const int N = int(1222); int n, a[N]; inline void gen() { return; } inline bool read() { if (!(cin >> n)) return false; forn1(i, n) assert(cin >> a[i]); return true; } const int MOD = INF + 7; inline int mul(int a, int b) { li res = a * 1LL * b; if (res >= MOD) res %= MOD; return int(res); } inline int sum(int a, int b) { int res = a + b; if (res >= MOD) res -= MOD; return res; } int binPow(int a, int n) { if (n == 0) return 1; if (n & 1) return mul(a, binPow(a, n - 1)); int res = binPow(a, n >> 1); return mul(res, res); } int f[N]; inline int C(int n, int k) { if (k > n) return 0; if (k == 0 || k == n) return 1; int num = f[n]; int denom = mul(f[k], f[n - k]); return mul(num, binPow(denom, MOD - 2)); } inline void add(int& a, int b) { a = sum(a, b); } int c[N][N]; int dp[N][N]; inline void solve() { f[0] = 1; for (int i = 1; i < N; i++) f[i] = mul(f[i - 1], i); forn(i, N) forn(j, N) c[i][j] = C(i, j); dp[1][1] = 1; bool sorted = true; for (int i = 2; i <= n; i++) { if (a[i] < a[i - 1]) sorted = false; if (sorted) dp[i][i] = 1; } set s; forn1(i, n) s.insert(a[i]); assert(sz(s) == n); for (int i = 1; i < n; i++) for (int j = 1; j <= i; j++) { int cur = dp[i][j]; if (cur == 0) continue; //cerr << "i j cur == " << i << ' ' << j << ' ' << cur << endl; bool sorted = true; int bound = min(n, i + j); for (int ni = i + 1; ni <= bound; ni++) { int nj = ni - (i + 1) + 1; assert(nj <= j); if (ni > i + 1 && a[ni] < a[ni - 1]) sorted = false; if (!sorted) break; //cerr << "ni == " << ni << endl; //cerr << "C == " << c[j][nj] << endl; //cerr << "toAdd == " << mul(cur, c[j][nj]) << endl; add(dp[ni][nj], mul(cur, mul(c[j][nj], f[nj]))); } } int ans = 0; forn1(i, n) add(ans, dp[n][i]); cout << ans << endl; return; } int main() { #ifdef _DEBUG assert(freopen("input.txt", "rt", stdin)); assert(freopen("output.txt", "wt", stdout)); #endif cout << setprecision(10) << fixed; cerr << setprecision(10) << fixed; srand(int(time(NULL))); int T = 1; //#define MULTITEST #ifdef MULTITEST assert(scanf("%d", &T) == 1); #endif forn(i, T) { #ifdef _DEBUG cerr << "TEST == " << i << endl; #endif assert(read()); //cout << "Case #" << i + 1 << ": "; solve(); //cerr << "curTime == " << clock() << " ms" << endl; } #ifdef _DEBUG cerr << "TIME == " << clock() << " ms" << endl; #endif return 0; }