1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include<bits/stdc++.h> using namespace std; void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; } void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');} #define lf double #define ll long long #define pii pair<int,int > #define vec vector<int > #define pb push_back #define mp make_pair #define fr first #define sc second #define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) const int maxn = 2000+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 998244353; int n,fac[maxn],f[maxn],ifac[maxn],v[maxn],a[maxn][maxn],g[maxn][maxn],w[maxn]; struct BIT { int t[maxn]; void clear() {memset(t,0,sizeof t);} void modify(int x,int v) {for(;x<=n;x+=x&-x) t[x]+=v;} int query(int x,int ans=0) {for(;x;x-=x&-x) ans+=t[x];return ans;} }T,r; int c(int aa,int b) {return 1ll*fac[aa]*ifac[b]%mod*ifac[aa-b]%mod;} int qpow(int aa,int x) { int res=1; for(;x;x>>=1,aa=1ll*aa*aa%mod) if(x&1) res=1ll*res*aa%mod; return res; } void gen() { fac[0]=ifac[0]=1; for(int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod; for(int i=1;i<=n;i++) ifac[i]=1ll*ifac[i-1]*qpow(i,mod-2)%mod; g[0][0]=1; for(int i=1;i<=n;i++) { g[i][0]=fac[i]; for(int j=1;j<=i;j++) g[i][j]=(g[i][j-1]-g[i-1][j-1]+mod)%mod; } f[0]=1; for(int i=1;i<=n;i++) f[i]=1ll*g[n][n]*f[i-1]%mod; } void del(int x) {if(v[x]) T.modify(x,-1),v[x]=0;} void delr(int x) { if(w[x]) w[x]=0,r.modify(x,-1); } int main() { read(n),gen();int ans=0; for(int i=1,x;i<=n;i++) { T.clear();r.clear(); for(int j=1;j<=n;j++) w[j]=v[j]=1,T.modify(j,1),r.modify(j,1); for(int j=1;j<=n;j++) { read(x),a[i][j]=x; if(i==1) { if(j>1) del(a[i][j-1]); ans=(ans+1ll*T.query(x-1)*f[n-i]%mod*g[n-j][0]%mod)%mod; continue; } if(j>1) del(a[i][j-1]),delr(a[i][j-1]); if(i>1) delr(a[i-1][j]); int p=r.query(x-1);if(a[i-1][j]<x&&w[a[i-1][j]]) p--; ans=(ans+1ll*p*f[n-i]%mod*g[n-j][r.query(n)-1]%mod)%mod; p=T.query(x-1)-r.query(x-1);if(a[i-1][j]<x&&v[a[i-1][j]]&&!w[a[i-1][j]]) p--; ans=(ans+1ll*p*f[n-i]%mod*g[n-j][r.query(n)]%mod)%mod; } }write(ans); return 0; }
|