#include #include #include #include #include using namespace std; typedef long long int64; const int64 M = 1000000007; int64 inv(long long m) { m = m % M; int64 p = M; int64 q = m; int64 a = 1; int64 b = 0; int64 c = 0; int64 d = 1; while (q > 0) // p > q { int64 r = p % q; int64 s = p / q; p = q; q = r; int64 x = a - s * c; int64 y = b - s * d; a = c; b = d; c = x; d = y; } while (b < 0) b += M; return b; } vector p, d, f, fi; vector> W; int64 n; // s number of numbers left // l last column // Q current number int64 choose(int64 n, int64 k) { int64 r = f[n]; // r = (r * fi[k]) % M; r = (r * fi[n-k]) % M; return r; } int64 next(int64 s, int64 l) { if (s == 0 || l == 1) return 1; if (W[s][l] !=-1) return W[s][l]; // ok s > 0 here, we need to proceed further int64 mm = min(l, s); mm = min(mm, d[n-s]); int64 res = 0; for (int64 ll = 1; ll <= mm; ll++) { int64 qq = choose(l, ll) * next(s - ll, ll); qq %= M; res = (res + qq) % M; /* int64 QQ = (Q * fi[l-ll]) % M; QQ = (QQ * f[ll]) % M; next(s - ll, ll, QQ);*/ } W[s][l] = res; return res; } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ cin >> n; p.resize(n); // numbers after last fall d.resize(n); f.resize(n+1); fi.resize(n+1); for (int64 i = 0; i < n; i++) cin >> p[i]; d[n-1] = 1; for (int64 i = n- 2; i >= 0; i--) if (p[i] < p[i+1]) d[i] = d[i+1] + 1; else d[i] = 1; f[0] = 1; f[1] = 1; fi[0] = 1; fi[1] = 1; for (int64 i = 2; i <= n; i++) { f[i] = (f[i - 1] * i) % M; fi[i] = inv(f[i]); } int64 R = 0; W.resize(n, vector(n,-1)); for (int64 i = 1; i <= d[0]; i++ ) R = (R + next(n-i,i)) % M; cout << R << endl; return 0; }