#include #define x first #define pb push_back #define mp make_pair #define y second #define all(a) a.begin(), a.end() #define l(x) (x<<1) #define r(x) ((x<<1) | 1) #define f(x) x>>1 #define lsb(x) (x&(-x)) using namespace std; typedef long long LL; typedef long double LD; typedef vector VLL; typedef pair PII; typedef pair PLL; typedef vector VI; typedef VI::iterator vit; typedef tuple PIII; const LL INF=1e6; const int NMAX=2255; const int LOGMAX=18; const int MOD=1000000007; const LD pi=acos(-1.0); LL n, A[NMAX], dp[NMAX][NMAX], m[NMAX], fact[NMAX], bin[NMAX][NMAX]; void solve(){ cin>>n; for(int i=1; i<=n; ++i) cin>>A[i]; fact[0]=1; for(int i=1; i<=n; ++i){ fact[i]=(1LL*i*fact[i-1])%MOD; } for(int i=0; i<=n; ++i) bin[i][0]=1; for(int i=1; i<=n; ++i){ for(int j=1; j<=n; ++j) { bin[i][j]=(bin[i-1][j] + bin[i-1][j-1])%MOD; } } A[0]=1000000; for(int i=1; i<=n; ++i) { if(A[i] < A[i-1]) m[i]=1; m[i]+=m[i-1]; } dp[0][0]=1; for(int j=1; j<=n; ++j) { for(int i=1; i<=j; ++i) { if(m[n-j+i] - m[n-j] == 0 || ((m[n-j+i] - m[n-j] == 1) && (m[n-j+1]-m[n-j])==1)){ for(int k=0; k<=i; ++k) dp[i][j]=(dp[i][j] + (1LL* ((1LL * bin[i][k] * fact[k])%MOD) * dp[k][j-i])%MOD)%MOD; } //cout<