# 题解

$$\begin{eqnarray*}dp[x][i][j]&=&\left (\frac{(i-1)i}2 + \frac{(j-i+1)(j-i+2)}2 + \frac {(n-j)(n-j+1)}2 \right )dp[x-1][i][j] \\ &+& \sum_{k=L}^{i-1} dp[x-1][k][j] \cdot (k-1) \\ &+& \sum_{k=j+1}^R dp[x-1][i][k] \cdot (n-k) \end{eqnarray*}$$

# 代码

#pragma GCC optimize("Ofast","inline")
#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof (x))
#define For(i,a,b) for (int i=a;i<=b;i++)
#define Fod(i,b,a) for (int i=b;i>=a;i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define _SEED_ ('C'+'L'+'Y'+'A'+'K'+'I'+'O'+'I')
#define outval(x) printf(#x" = %d\n",x)
#define outvec(x) printf("vec "#x" = ");for (auto _v : x)printf("%d ",_v);puts("")
#define outtag(x) puts("----------"#x"----------")
#define outarr(a,L,R) printf(#a"[%d...%d] = ",L,R);\
For(_v2,L,R)printf("%d ",a[_v2]);puts("");
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> vi;
LL x=0,f=0;
char ch=getchar();
while (!isdigit(ch))
f|=ch=='-',ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
const int N=405,mod=1e9+7,INF=mod;
if ((x+=y)>=mod)
x-=mod;
}
void Del(int &x,int y){
if ((x-=y)<0)
x+=mod;
}
int Pow(int x,int y){
int ans=1;
for (;y;y>>=1,x=(LL)x*x%mod)
if (y&1)
ans=(LL)ans*x%mod;
return ans;
}
int n,q;
int a[N];
int val[N][N];
vector <int> Ha;
int tmp[N][N];
int dp[2][N][N];
void solve(int L,int R,int t){
For(i,L,R)
For(j,i,R)
dp[0][i][j]=0;
dp[0][L][R]=1;
For(c,1,q){
int _0=(c^1)&1,_1=c&1;
For(i,L,R)
For(j,i,R)
dp[_1][i][j]=(LL)tmp[i][j]*dp[_0][i][j]%mod;
For(j,L,R){
int s=0;
For(i,L,j){
}
}
For(i,L,R){
int s=0;
Fod(j,R,i){
}
}
}
For(j,L,R){
int s=0;
For(i,L,j){
}
}
}
int main(){
For(i,1,n)
sort(Ha.begin(),Ha.end());
Ha.erase(unique(Ha.begin(),Ha.end()),Ha.end());
For(i,1,n)
a[i]=lower_bound(Ha.begin(),Ha.end(),a[i])-Ha.begin();
For(i,1,n)
For(j,1,n)
tmp[i][j]=(i-1)*i/2+(j-i+1)*(j-i+2)/2+(n-j)*(n-j+1)/2;
a[0]=a[n+1]=Ha.size();
For(t,0,(int)Ha.size()-1){
int L=0,Mx=0;
For(R,1,n+1)
if (a[R]<=t)
Mx=max(Mx,a[R]);
else {
if (L+1<=R-1){
if (Mx<t){
For(i,L+1,R-1)
}
else
solve(L+1,R-1,t);
}
L=R,Mx=0;
}
}
For(i,1,n){
int ans=0;
Fod(j,(int)Ha.size()-1,1)
Del(val[i][j],val[i][j-1]);
For(j,0,(int)Ha.size()-1)