#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); i++) #define repd(i, a, b) for(int i = (a); i > (b); i--) #define forIt(it, a) for(__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define forRev(it, a) for(__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++) #define ft(a) __typeof((a).begin()) #define ll long long #define ld long double #define fi first #define se second #define mk make_pair #define pb push_back #define sz(a) (int)(a).size() #define all(a) (a).begin(), (a).end() #define Rep(i,n) for(int i = 0; i < (n); ++i) #define bitcount(n) __builtin_popcountll(n) #define randchar() ((rand() % 26) + 'a') typedef complex cplex; typedef vector vi; typedef pair ii; typedef pair iii; typedef vector vii; typedef vector viii; const int N = 1200 + 7; const int M = N; const int inf = 1e9 + 7; const long long linf = 1ll * inf * (inf - 1); const double pi = acos(-1); const double eps = 1e-7; const bool multipleTest = 0; int lt[N], invlt[N]; int inverse(int x, int n){ int r = n, newr = x; int t = 0, newt = 1; while (newr > 0){ int q = r / newr; int tmp = newr; newr = r % newr; r = tmp; tmp = newt; newt = t - q * newt; t = tmp; } if (t < 0) t += n; return t; } void init() { lt[0] = invlt[0] = 1; rep(i, 1, N) { lt[i] = (ll) lt[i - 1] * i % inf; invlt[i] = inverse(lt[i], inf); } } ll DP[N][M]; int n; int a[N]; void solve() { cin >> n; for(int i = 1; i <= n; ++i ) { cin >> a[i]; } init(); for(int i = n; i > 0; --i) { bool ok = true; for(int j = i; j <= n && j - i + 1 < M; ++j) { if (j > i && a[j] < a[j - 1]) ok = false; if (!ok) break; int k = j - i + 1; if (j == n) DP[i][k] = 1; else { for(int t = 1; t <= k; ++t) { DP[i][k] += DP[j + 1][t] * lt[k] % inf * invlt[k - t]; if (DP[i][k] >= linf) DP[i][k] -= linf; } } DP[i][k] %= inf; } } ll ans = 0; for(int i = 1; i < M; ++i) ans = (ans + DP[1][i]) % inf; cout << ans << '\n'; } void test() { freopen("in.txt", "w", stdout); for(int i = 0; i < 50000; ++i) { printf("%c", randchar()); } cout << '\n' << 100000 << '\n'; rep(i, 0, 100000) { rep(t, 0, (rand() % 4) + 1) printf("%c", randchar()); printf(" "); rep(t, 0, (rand() % 4) + 1) printf("%c", randchar()); printf("\n"); } } int main() { #ifdef _LOCAL_ freopen("in.txt", "r", stdin); #endif // freopen("out.txt", "w", stdout); int Test = 1; if (multipleTest) { cin >> Test; } for(int i = 0; i < Test; ++i) { // printf("Case #%d: ", i + 1); solve(); } #ifdef _LOCAL_ cout<<"\n" << 1.0 * clock() / CLOCKS_PER_SEC<