#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; namespace fastinput { /** Interface */ inline int readChar(); template inline T readInt(); template inline void writeInt(T x, char end = 0); inline void writeChar(int x); inline void writeWord(const char *s); /** Read */ static const int buf_size = 16384; inline int getChar() { static char buf[buf_size]; static int len = 0, pos = 0; if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin); if (pos == len) return -1; return buf[pos++]; } inline int readChar() { int c = getChar(); while (c <= 32) c = getChar(); return c; } template inline T readInt() { int s = 1, c = readChar(); T x = 0; if (c == '-') s = -1, c = getChar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); return s == 1 ? x : -x; } /** Write */ static int write_pos = 0; static char write_buf[buf_size]; inline void writeChar(int x) { if (write_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; write_buf[write_pos++] = x; } template inline void writeInt(T x, char end) { if (x < 0) writeChar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = (char)('0' + x % 10), x /= 10; while (n--) writeChar(s[n]); if (end) writeChar(end); } inline void writeWord(const char *s) { while (*s) writeChar(*s++); } struct Flusher { ~Flusher() { if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; } } flusher; } using namespace fastinput; const int N = 1300; const int MOD = 1e9 + 7; inline void add(int &a, int b) { a += b; if (a >= MOD) a -= MOD; } inline void mult(int &a, int b) { a = ((long long)a * b) % MOD; } inline int multed(int a, int b) { return ((long long)a * b) % MOD; } //we will think that size[1] >= size[2] >= ... >= size[last] int dp[N][N]; int factorial[N]; int revfact[N]; int last[N]; int m[N]; int n; bool sorted(int i, int j) { return last[i] >= j; } int binpow(int a, int deg) { if (deg == 0) return 1; if (deg & 1) return multed(binpow(multed(a, a), deg / 2), a); else return binpow(multed(a, a), deg / 2); } int calc(int prefix, int left) { //cerr << prefix << " " << left << endl; if (dp[prefix][left] != -1) return dp[prefix][left]; dp[prefix][left] = 0; if (!sorted(prefix, prefix + left)) // { assert(false); return 0; } if (prefix + left == n) return dp[prefix][left] = 1; int from = prefix + left; for (int i = 1; i <= left && from + i <= n; ++i) { int to = from + i; if (!sorted(from, to)) break; calc(from, i); add(dp[prefix][left], multed(multed(dp[from][i], factorial[left]), revfact[left - i])); } return dp[prefix][left]; } void precalc() { for (int l = 0; l < n; ) { for (int r = l + 1; r <= n; ++r) if (r == n || m[r] < m[r - 1]) { for (int j = l; j < r; ++j) last[j] = r; l = r; } } memset(dp, -1, sizeof(dp)); factorial[1] = 1; for (int i = 2; i <= n; ++i) factorial[i] = multed(factorial[i - 1], i); revfact[0] = 1; revfact[1] = 1; for (int i = 2; i <= n; ++i) revfact[i] = binpow(factorial[i], MOD - 2); } int main() { n = readInt(); for (int i = 0; i < n; ++i) m[i] = readInt(); precalc(); int sum = 0; for (int i = 1; i <= n; ++i) { if (!sorted(0, i)) break; add(sum, calc(0, i)); } cout << sum << endl; return 0; }