# CF1086E Beautiful Matrix

### codeforces

1、那么这个位置放这$$x$$个数就会导致前$$j$$个数一共只剩下$$x-1$$个数不同，也就是后$$n-j$$个数有$$n-j-x+1$$个限制

2、假如不放这$$x$$个数，依然是$$x$$个限制

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define rg register
char ch;bool ok;
for(ok=0,ch=getchar();!isdigit(ch);ch=getchar())if(ch=='-')ok=1;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok)x=-x;
}
const int maxn=2010,mod=998244353;bool vis[maxn],used[maxn];
int n,a[maxn][maxn],f[maxn][maxn],fac[maxn],ans;
int mul(int x,int y){return 1ll*x*y-1ll*x*y/mod*mod;}
int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
int del(int x,int y){return x-y<0?x-y+mod:x-y;}
int g[2][maxn],ff[maxn];
#define lowbit(i) (i&(-i))
void ins(int a,int x,int z){for(rg int i=x;i<=n;i+=lowbit(i))g[a][i]+=z;}
int get(int a,int x){int ans=0;for(rg int i=x;i;i-=lowbit(i))ans=add(ans,g[a][i]);return ans;}
int main(){
for(rg int i=1;i<=n;i++)fac[i]=mul(fac[i-1],i);
for(rg int i=1;i<=n;i++)
for(rg int j=1;j<=n;j++)
f[0][0]=1;
for(rg int i=1;i<=n;i++){
f[i][0]=fac[i];
for(rg int j=1;j<=i;j++)
f[i][j]=del(f[i][j-1],f[i-1][j-1]);
}
ff[0]=1;
for(rg int i=1;i<=n;i++)ff[i]=mul(ff[i-1],f[n][n]);
for(rg int j=1;j<=n;j++){
int t=0;
for(rg int i=1;i<a[1][j];i++)if(!vis[i])t++;
vis[a[1][j]]=1;
}
memset(vis,0,sizeof vis);
for(rg int i=2;i<=n;i++){
for(rg int j=1;j<=n;j++){

int t=a[i][j]-1-get(0,a[i][j]-1),h=get(1,a[i][j]-1);
if(!vis[a[i-1][j]]&&a[i-1][j]<a[i][j])t--;
if(!vis[a[i-1][j]])ins(1,a[i-1][j],1);vis[a[i][j]]=1;