#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=405,sqrn=405;
const ll mod=998244353;
int n;
ll dp[sqrn][N][N];
ll a[N][N];
ll tj[N*N];
ll tag[N],len;
vector<int> gx[N*N],gy[N*N];
ll pf[N][N];
int main(){
cin >>n;
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
for(int k=1;k<=400;k++)dp[k][i][j]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
gx[a[i][j]].push_back(i);
gy[a[i][j]].push_back(j);
tj[a[i][j]]++;
if(i==1 && j==1)pf[i][j]=1;
else pf[i][j]=(pf[i-1][j]+pf[i][j-1])%mod;
}
}
for(int i=1;i<=n*n;i++){
if(tj[i]>400)tag[i]=++len;
}
ll ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!tag[a[i][j]]){
for(int k=0;k<gx[a[i][j]].size();k++){
int dx=i-gx[a[i][j]][k],dy=j-gy[a[i][j]][k];
if(dx>=0 &&dy>=0){
ans=(ans+pf[dx+1][dy+1])%mod;
}
}
}else{
dp[tag[a[i][j]]][i][j]=1;
dp[tag[a[i][j]]][i][j]+=dp[tag[a[i][j]]][i-1][j];
dp[tag[a[i][j]]][i][j]+=dp[tag[a[i][j]]][i][j-1];
dp[tag[a[i][j]]][i][j]%=mod;
ans=(ans+dp[tag[a[i][j]]][i][j])%mod;
}
}
}
cout<<ans;
return 0;
}