#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;

    int q;
    cin >> q;
    vector<vector<int>> queries(q, vector<int>(3));
    rep(i,q){
        int t, a, b;
        cin >> t >> a >> b;
        a--;b--;
        queries[i] = {t, a, b};
    }

    bool flip=false;
    for(auto query : queries){
        if(query[0]==2){
            flip = !flip;            
        }
        if(query[0]==1){
            if(flip){
                query[1]+=n;
                query[1]%=2*n;
                query[2]+=n;
                query[2]%=2*n;
            }
            char tmp;
            tmp = s[query[1]];
            s[query[1]] = s[query[2]];
            s[query[2]] = tmp;
        }
    }
    if(flip){
        s = s.substr(n) + s.substr(0, n);
    }
    cout << s << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
 
//#define kk
#define int long long
#define mat vector<vector<int>>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define pi pair<int, int>
#define inf 1000000000
#define mod 998244353
#define endl "\n"

const ll INF=1e18+5;

const int naxN=55;
int fact[naxN], inv_fact[naxN];

int power(int a, int n){
	int res=1;
	
	while(n){
		if(n%2) res=(res*a)%mod, n--;
		else a=(a*a)%mod, n/=2;
	}
	return res;
}

void init(){
	fact[0]=inv_fact[0]=1;
	
	for(int i=1; i<naxN; i++){
		fact[i]=(i*fact[i-1])%mod;
		inv_fact[i]=power(fact[i], mod-2)%mod;
	}
}

int ncr(int a, int b){
	if(a<0 || b<0 || a<b) return 0;
	
	return (fact[a]%mod*inv_fact[b]%mod*inv_fact[a-b]%mod)%mod;
}

int dx[8]={-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8]={-1, 0, 1, 1, 1, 0, -1, -1};



//****DO NOT TOUCH ABOVE THIS LINE****//

const int maxN=2e5+5;

#define M 998244353

int arr[55][55];

int par[55];
int R[55];

int find(int a){
	if(par[a]<0) return a;
	return par[a]=find(par[a]);
}

void merge(int a, int b){
	a=find(a);
	b=find(b);
	
	if(a==b) return;
	
	if(R[a]>=R[b]){
		par[b]=a;
		R[a]+=R[b];
	}
	else{
		par[a]=b;
		R[b]+=R[a];
	}
}

void solve(){
	int n, K;
	cin>>n>>K;
	
	for(int i=1; i<=n; i++){
		for(int j=1; j<=n; j++){
			cin>>arr[i][j];
		}
	}
	
	for(int i=1; i<=n; i++){
		par[i]=-1;
		R[i]=1;
	}
	
	for(int i=1; i<=n; i++){
		for(int j=i+1; j<=n; j++){
			int can=1;
			for(int k=1; k<=n; k++){
				if(arr[i][k]+arr[j][k]>K){
					can=0;
					break;
				}
			}
			
			if(can) merge(i, j);
		}
	}
	
	init();
	
	int ans1=1;
	
	for(int i=1; i<=n; i++){
		if(par[i]==-1) ans1*=fact[R[i]]%=M;
	}
	
	for(int i=1; i<=n; i++){
		par[i]=-1;
		R[i]=1;
	}
	
	for(int i=1; i<=n; i++){
		for(int j=i+1; j<=n; j++){
			int can=1;
			for(int k=1; k<=n; k++){
				if(arr[k][i]+arr[k][j]>K){
					can=0;
					break;
				}
			}
			
			if(can) merge(i, j);
		}
	}
	
	int ans2=1;
	
	for(int i=1; i<=n; i++){
		if(par[i]==-1) ans2*=fact[R[i]]%=M;
	}
	
	int ans=(ans1*ans2)%M;
	
	cout<<ans<<endl;
}

//****DO NOT TOUCH BELOW THIS LINE****//

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cout<<fixed<<setprecision(6);
	
	#ifdef kk
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#endif
	
	int T=1;
	//cin>>T;

	while(T--){		
		solve();
	}
}
