#include using namespace std; typedef long long ll; const int MAXN = 1200 + 5; const int MOD = 1e9 + 7; int n, a[MAXN], ch[MAXN][MAXN], pt[MAXN], d[MAXN][MAXN], ss[MAXN]; int main(){ ios::sync_with_stdio(false); cin.tie(0); ch[0][0] = 1; for (int i = 1; i < MAXN; i++){ ch[i][0] = ch[i][i] = 1; for (int j = 1; j < i; j++){ ch[i][j] = ch[i - 1][j] + ch[i - 1][j - 1]; while (ch[i][j] >= MOD) ch[i][j] -= MOD; } } ss[0] = 1; for (int i = 1; i < MAXN; i++) ss[i] = 1ll*ss[i-1]*i%MOD; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) pt[i] = pt[i - 1] + int(a[i] < a[i - 1]); d[n][0] = 1; for (int i = n-1; ~i; i--) for (int j = 1; i+j <= n && pt[i+j-1] - pt[i] == 0; j++) for (int w = 0; i+j+w <= n && w <= j; w++) d[i][j] = (d[i][j] + 1ll*ss[w]*ch[j][w]%MOD*d[i + j][w]) % MOD; int ans = 0; for (int i = 1; i <= n; i++){ ans += d[0][i]; while (ans >= MOD) ans -= MOD; } cout << ans << "\n"; return 0; }