#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 f first #define s second #define pb push_back #define pp pop_back #define mp make_pair #define ll long long #define ld double #define ull unsigned long long #define PI pair < int, int > const int N = 1234; const int M = 123; const ld Pi = acos(-1); const ll Inf = 1e18; const int inf = 1e9; const int mod = 1e9 + 7; void add(int &a, int b) { a += b; if (a >= mod) a -= mod; } int mult(int a, int b) { return 1ll * a * b % mod; } int sum(int a, int b) { add(a, b); return a; } int n, a[N], dp[N][N], cnk[N][N], f[N]; int main() { #ifdef wws freopen("in", "r", stdin); // freopen("out", "w", stdout); #endif ios_base::sync_with_stdio(0); cin >> n; for (int i = 0;i <= n;i++) { cnk[i][i] = cnk[i][0] = 1; if (i == 0) f[i] = 1; else f[i] = mult(i, f[i - 1]); for (int j = 1;j < i;j++) { cnk[i][j] = sum(cnk[i - 1][j], cnk[i - 1][j - 1]); } } for (int i = 1;i <= n;i++) { cin >> a[i]; } reverse(a + 1, a + n + 1); dp[0][0] = 1; for (int i = 0;i <= n;i++) { for (int j = 0;j <= i;j++) { int lst = inf; for (int k = i + 1;k <= n;k++) { if (lst < a[k]) { break; } lst = a[k]; // cout << "k = " << k << endl; if (k - i >= j) { add(dp[k][k - i], mult(mult(cnk[k - i][j], f[j]), dp[i][j])); } } // cout << i << " " << j << " dp = " << dp[i][j] << endl; } } int ans = 0; for (int i = 0;i <= n;i++) { add(ans, dp[n][i]); } cout << ans << endl; return 0; }