#include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef std::pair ii; ///////////////////////////////////////////////////////// BEGIN OF MODULAR INTEGER ///////////////////////////////////////////////////////// //TODO: commutativity (int*mint) //TODO: ostream template class ModularInteger { private: long long value; static long long inverse(long long a) { long long b = MOD; long long b0 = b, t, q; long long x0 = 0, x1 = 1; if (b == 1) return 1; while (a > 1) { q = a / b; t = b, b = a % b, a = t; t = x0, x0 = x1 - q * x0, x1 = t; } if (x1 < 0) x1 += b0; return x1; } public: static_assert(MOD >= 2, "MOD must be at least two."); ModularInteger() : value(0) {} ModularInteger(const long long value) : value(value >= 0 ? value % MOD : (MOD - ((-value)%MOD))%MOD) {} ModularInteger operator^=(long long exp) { if (exp == 0) { value = 1; } else { long long acc = 1; while (exp > 1) { if (exp&1) { acc *= value; acc %= MOD; } value *= value; value %= MOD; exp >>= 1; } value *= acc; value %= MOD; } return *this; } inline ModularInteger& operator+=(const ModularInteger& rhs) { value += rhs.value; value %= MOD; return *this; } inline ModularInteger& operator-=(const ModularInteger& rhs) { value += MOD - rhs.value; value %= MOD; return *this; } inline ModularInteger& operator*=(const ModularInteger& rhs) { value *= rhs.value; value %= MOD; return *this; } inline ModularInteger& operator/=(const ModularInteger& rhs) { value *= inverse(rhs.value); value %= MOD; return *this; } inline ModularInteger operator+(const ModularInteger& rhs) const { return ModularInteger(*this) += rhs; } inline ModularInteger operator-(const ModularInteger& rhs) const { return ModularInteger(*this) -= rhs; } inline ModularInteger operator*(const ModularInteger& rhs) const { return ModularInteger(*this) *= rhs; } inline ModularInteger operator/(const ModularInteger& rhs) const { return ModularInteger(*this) /= rhs; } inline ModularInteger operator^(const long long exp) const { return ModularInteger(*this) ^= exp; } inline long long to_ll() { return value; } inline int to_int() { return value; } }; //typedef ModularInteger<0> mint; typedef ModularInteger<1000000000 + 7> mint; //typedef ModularInteger<1000000000 + 9> mint; ///////////////////////////////////////////////////////// END OF MODULAR INTEGER ///////////////////////////////////////////////////////// ///////////////////////////////////////////////////////// BEGIN OF COMBINATORICS ///////////////////////////////////////////////////////// class Combinatorics { public: Combinatorics(const int max_x) : fat(max_x+1) , antifat(max_x+1) { fat[0] = 1; for (int x = 1; x <= max_x; ++x) fat[x] = fat[x-1] * x; antifat[0] = 1; for (int x = 1; x <= max_x; ++x) antifat[x] = antifat[x-1] / x; } inline mint choose(const int n, const int k) { return fat[n] * (antifat[k] * antifat[n-k]); } // Number of k-tuples of non-negative integers whose sum is n inline mint choose_tuple(const int n, const int k) { if (k == 0) return n == 0 ? 1 : 0; return choose(n+k-1, n); } std::vector fat; std::vector antifat; }; ///////////////////////////////////////////////////////// END OF COMBINATORICS ///////////////////////////////////////////////////////// const int MAX_N = 1200; int n; int v[MAX_N]; bool ordered_until[MAX_N][MAX_N]; Combinatorics comb(10000); mint dp[MAX_N][MAX_N+1]; bool saved[MAX_N][MAX_N+1]; mint solve(int i, int piles) { //printf("begin of (%d, %d)\n", i ,piles); if (piles == 0) return i == n ? 1 : 0; if (i + piles > n) return 0; if (!ordered_until[i][i+piles-1]) return 0; //if (i+piles == n) //return 1; if (!saved[i][piles]) { saved[i][piles] = true; for (int piles2 = 0; piles2 <= piles; ++piles2) dp[i][piles] += comb.fat[piles]*solve(i+piles, piles2)*comb.choose(piles, piles2); } //printf("solve(%d, %d) = %d\n", i, piles, dp[i][piles].to_int()); return dp[i][piles]; } int32_t main() { cin>>n; for (int i = 0; i < n; ++i) cin>>v[i]; set distinct; for (int i = 0; i < n; ++i) distinct.insert(v[i]); assert((int)distinct.size() == n); for (int i = 0; i < n; ++i) { ordered_until[i][i] = true; for (int j = i+1; j < n; ++j) { if (v[j-1] > v[j]) break; ordered_until[i][j] = true; } } mint ans = 0; for (int i = 1; i <= n; ++i) ans += solve(0, i) / comb.fat[i]; cout<