#define _GLIBCXX_DEBUG #include using namespace std; #define pb push_back #define mp make_pair #define fst first #define snd second #define forn(i, n) for (int i = 0; i < int(n); ++i) typedef long long ll; typedef vector vi; typedef vector vvi; typedef vector vll; typedef pair pii; typedef vector vii; #define sz(c) (int)(c).size() #define all(c) (c).begin(), (c).end() const int mod = (int)1e9 + 7; void add (int &a, int b) { a += b; if (a >= mod) a -= mod; } void sub (int &a, int b) { a -= b; if (a < 0) a += mod; } int mult (int a, int b) { return (ll)a * b % mod; } void solve (int n) { vi m(n); forn (i, n) cin >> m[i]; vi inv(n + 1, 1), fact(n + 1, 1), ifact(n + 1, 1); for (int i = 2; i <= n; i++) { inv[i] = mod - mult(mod / i, inv[mod % i]); assert(mult(i, inv[i]) == 1); fact[i] = mult(fact[i - 1], i); ifact[i] = mult(ifact[i - 1], inv[i]); } vector> srt(n + 1, vector(n + 1)); //char srt[n + 1][n + 1]; //memset(srt, 0, sizeof srt); forn (i, n + 1) srt[i][i] = 1; forn (j, n + 1) for (int i = j - 1; i >= 0; i--) srt[i][j] = (srt[i + 1][j] && (i + 1 == j || m[i] <= m[i + 1])); vvi dp(n + 1, vi(n + 1)); //int dp[n + 1][n + 1]; //memset(dp, 0, sizeof dp); forn (i, n + 1) if (srt[0][i] && i > 0) dp[i][i] = 1; forn (i, n + 1) forn (j, n + 1) if (dp[i][j]) { assert(j > 0); int cur = dp[i][j]; forn (nj, j + 1) if (nj != 0) { if (i + nj > n || !srt[i][i + nj]) break; int mlt = mult(fact[j], ifact[j - nj]); add(dp[i + nj][nj], mult(cur, mlt)); } } int ans = 0; forn (i, n + 1) add(ans, dp[n][i]); cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; while (cin >> n) solve(n); }