#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; mint dp[5010][5010]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t){ t--; int i,j,n; cin >> n; vector a(n); for(i=0;i> a[i]; vector b(n + 1,-1); for(i=0;i=2) dp[i][j] += dp[i - 1][j - 2]; } dp[i][j] += dp[i - 1][j]*(j - i + 1); }else{ if(b[i]==j){ dp[i][j] += dp[i - 1][j - 1]; if(j>=2) dp[i][j] += dp[i - 1][j - 2]; } if(b[i]<=j) dp[i][j] += dp[i - 1][j]; } } } mint ans = dp[n][n]; cout << ans.val() << "\n"; } }