/* ______ || // | _____| || // || // || || // ||// ||_____ ||// ||\\ | _____| ||\\ || \\ || || \\ || \\ ||_____ || \\ || \\ |______| || \\ */ #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int N6 = 1e6 + 6, N3 = 2e3 + 6, oo = 1e9 + 9, base = 1e9 + 7; const long long ool = 1e18 + 9; typedef unsigned long long ull; typedef long long ll; typedef double ld; typedef pair PII; typedef pair PLL; #define F first #define S second #define pb push_back #define eb emplace_back #define right(x) x << 1 | 1 #define left(x) x << 1 #define forn(x, a, b) for (int x = a; x <= b; ++x) #define for1(x, a, b) for (int x = a; x >= b; --x) #define mkp make_pair #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() #define y1 kekekek int n, pos[N3]; ll fact[N3], a[N3], cnk[N3][N3], d[N3][N3]; ll mult(ll a, ll b) { return (a * b) % base; } int main() { ios_base :: sync_with_stdio(0); cin.tie(0); cin >> n; fact[0] = 1ll; forn(i, 1, n) { cin >> a[i]; if (i == 1 || a[i] < a[i - 1]) pos[i] = i; else pos[i] = pos[i - 1]; fact[i] = mult(fact[i - 1], i); } cnk[0][0] = 1ll; forn(i, 1, n) { cnk[i][0] = 1ll; forn(j, 1, i) { cnk[i][j] = (cnk[i - 1][j] + cnk[i - 1][j - 1]) % base; } } forn(i, 1, n) { if (pos[i] == 1) d[i][i] = 1; } forn(i, 1, n) { for1(j, i - pos[i] + (pos[i] != 1), 1) { forn(k, j, i - j) { d[i][j] = (d[i][j] + mult(d[i - j][k], mult(cnk[k][j], fact[j]))) % base; } } } ll ans = 0; forn(i, 1, n) ans = (ans + d[n][i]) % base; cout << ans << "\n"; return 0; }