#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define all(a) (a).begin(), (a).end() #define sz(a) (int)(a).size() #define pb push_back using namespace std; typedef complex Complex; typedef long long ll; typedef long double ld; mt19937 rr(random_device{}()); const int P = 1e9 + 7; const double PI = acos(-1); int n, k; vector rev; vector w; void precalc(int m) { k = 0; for (; (1 << k) < m; ++k); n = 1 << k; int h = -1; rev.resize(n); for (int i = 1; i < n; ++i) { if ((i & (i - 1)) == 0) ++h; rev[i] = rev[i ^ (1 << h)] ^ (1 << (k - h - 1)); } w.resize(n); for (int i = 0; i < n; ++i) { double alpha = 2 * PI * i / n; w[i] = {cos(alpha), sin(alpha)}; } } vector fft(vector &a) { vector cur(n), next(n); for (int i = 0; i < n; ++i) cur[i] = a[rev[i]]; for (int len = 1; len < n; len <<= 1) { int step = n / (2 * len); for (int pos = 0; pos < n; pos += len) { for (int i = 0; i < len; ++i, ++pos) { Complex val = w[i * step] * cur[pos + len]; next[pos] = cur[pos] + val; next[pos + len] = cur[pos] - val; } } swap(cur, next); } return cur; } vector fft_rev(vector &a) { vector res = fft(a); for (int i = 0; i < n; ++i) res[i] /= n; reverse(res.begin() + 1, res.end()); return res; } ll binPow(ll a, int b) { if (b == 0) return 1; ll x = binPow(a, b / 2); x *= x; x %= P; if (b & 1) { x *= a; x %= P; } return x; } vector mult(vector &a, vector &b) { ll x = sqrt(P); vector al(n), ar(n), bl(n), br(n); for (int i = 0; i < n; ++i) { al[i] = (i < sz(a) ? a[i] % x : 0); ar[i] = (i < sz(a) ? a[i] / x : 0); bl[i] = (i < sz(b) ? b[i] % x : 0); br[i] = (i < sz(b) ? b[i] / x : 0); } al = fft(al), ar = fft(ar), bl = fft(bl), br = fft(br); vector ff(n), fs(n), ss(n); for (int i = 0; i < n; ++i) { ff[i] = al[i] * bl[i]; fs[i] = (al[i] + ar[i]) * (bl[i] + br[i]); ss[i] = ar[i] * br[i]; } ff = fft_rev(ff); fs = fft_rev(fs); ss = fft_rev(ss); vector res(n); for (int i = 0; i < n; ++i) { ll u = (ll)round(ff[i].real()) % P; ll v = (ll)round(fs[i].real()) % P; ll w = (ll)round(ss[i].real()) % P; res[i] += u; res[i] += (v + 2 * P - u - w) * x % P; res[i] += w * x * x % P; res[i] %= P; } return res; } int main() { //freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; precalc(2 * n); vector a(n); // for (int i = 0; i < n; ++i) // a[i] = i + 1; // random_shuffle(all(a)); for (int i = 0; i < n; ++i) cin >> a[i]; vector len(n + 1); len[n - 1] = 1; for (int i = n - 2; i >= 0; --i) { len[i] = (a[i] < a[i + 1] ? len[i + 1] + 1 : 1); } vector fact(n + 1); fact[0] = 1; for (ll i = 1; i <= n; ++i) { fact[i] = fact[i - 1] * i; fact[i] %= P; } vector revFact(n + 1); for (int i = 0; i <= n; ++i) revFact[i] = binPow(fact[i], P - 2); vector> dp(n + 1, vector(n + 1)); for (int j = 1; j <= n; ++j) dp[n][j] = 1; for (int i = n - 1; i >= 0; --i) { vector p(n); for (int j = 1; j <= len[i]; ++j) p[j] = dp[i + j][j]; p = mult(p, revFact); for (int j = 1; j <= n; ++j) { dp[i][j] = p[j] * fact[j]; dp[i][j] %= P; } // if (i + len[i] == n) { // for (int j = n - i; j <= n; ++j) { // ++dp[i][j]; // if (dp[i][j] >= P) // dp[i][j] -= P; // } // } } ll ans = 0; for (int i = 0; i < len[0]; ++i) { ans += dp[i + 1][i + 1]; if (ans >= P) ans -= P; } cout << ans << endl; // double tm = clock(); // cout << tm / CLOCKS_PER_SEC << endl; }