//x<<y=x*2^y,x>>y=x/2^y
//1e5+3 is prime
// in a matrix, all elements of a primary diagonal have constant diff of coordinates
// and a secondary diagonal has constant sum of coordinates
//use ll() for using an integer in self-built fn
//(x&-x) bitwise and of (x and its 2's complement) returns (last set bit)
//eg if x=1010 then it will return 0010 
// careful dont print empty container(run time error) 
//v.erase O(n)
//use ("\n") instead of endl as endl flushes the output buffer
//every time so takes more time than \n (TLE)
//stoll() and to_string((less than 1e19))
//INT_MAX is approx 3e10
//For sets use set_name.lower_bound(x)(strictly O(logn)) 
//NOT lb(all(s),x)(O(logn) for random access (eg vector) but for sets it is O(n));
#include<bits/stdc++.h>
typedef long long int ll;
#define ull unsigned long long int
#define lld long double
#define endl "\n"
#define fi first 
#define sec second 
#define sp setprecision 
#define lb lower_bound
#define ub upper_bound
#define For(i, a, b) for(long long int i = (a); i <= (b); i++)
#define Forr(i, a, b) for(long long int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define vll vector<ll>
#define pll pair<ll,ll>
#define vlld vector<lld>
#define vi vector<int>
#define vch vector<char>
#define sll set<ll>
#define sch set<ch>
#define vpll vector< pair<ll,ll> >
#define vpii vector< pair<int,int> >
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define mll map<ll, ll>
#define mcll map<char,ll>
#define sz(container) ll((container).size())
#define fill(a,b) memset(a, b, sizeof(a))
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
lld pi=3.1415926535897932;
const ll mod=1000000007;
const ll dx[4]={-1, 0, 1, 0} , dy[4]={0, 1, 0, -1};
const ll dxx[8]={-1, -1, 0, 1, 1, 1, 0, -1} , dyy[8]={0, 1, 1, 1, 0, -1, -1, -1};
string ds="RLDU";
/***************************************
struct cmp 
{
    bool operator() (const pll &a, const pll &b)  const 
    {
        ll lena = a.sec - a.fi + 1;
        ll lenb = b.sec - b.fi + 1;
        if (lena == lenb) return a.fi < b.fi;
        return lena > lenb;
    }
};
********************************/



ll lcm(ll a, ll b)
{
    ll g=__gcd(a, b);
    return a/g*b;
}

ll binpow(ll a, ll b,ll m) 
{
    a %= m;
    ll res = 1;
    while (b > 0) 
    {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
ll modinv(ll n) 
{
    return binpow(n, mod - 2,mod);
}


/**************coding****************************************/
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fast_io;
    ll T=1;//cin>>T;
    For(i,1,T)
    {
        ll n;cin>>n;
        char caa,cab,cba,cbb;cin>>caa>>cab>>cba>>cbb;
        ll fib[n+1]={};
        fib[1]=2;fib[0]=1;
        For(j,2,n) fib[j]=(fib[j-1]+fib[j-2])%mod;
        if(cab=='B')
        {
            if(cab==cbb||n<4)
            {
                cout<<1;return 0;
            }
            if(cbb==cba)
            {
                cout<<binpow(2,n-3,mod);return 0;
            }
            if(cba=cab)
            {
                cout<<fib[n-3];
            }
        }
        else
        {
            if(cab==caa||n<4)
            {
                cout<<1;return 0;
            }
            if(caa==cba)
            {
                cout<<binpow(2,n-3,mod);return 0;
            }
            if(cba=cab)
            {
                cout<<fib[n-3];
            }
        }
    }
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define int ll
using ll = long long;
using P = pair<int, int>;
using int128 = __int128_t;//cin coutはできない
 
const int mod = 998244353;
struct mint{
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a){
        if((x+=a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a){
        if((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a){
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res *= a;
    }
    mint pow(ll t) const {
        if(!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if(t&1) a *= *this;
        return a;
    }
 
    mint inv() const {
        return pow(mod - 2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res /= a;
    }
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
 
 
struct UnionFind {
    //vector d
    //親のノードを記録する
    //親の場合は-(木の要素数)を記録する
    vector<int> d;
    UnionFind(int n = 0) : d(n, -1) {} //constructor
    int find(int x){
        //find root
        if(d[x] < 0){
            return x;
        }
        return d[x] = find(d[x]);
    }
    bool unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y)
            return false;
        //xの木の方が、yの木よりも小さくないといけない
        if(-d[x] > -d[y]){
            swap(x, y);
        }
        d[x] += d[y];//yの木の要素数をxの木の要素数に合算する
        d[y] = x;//yの親をxにする
        return true;
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
    int size(int x){
        return -d[x];
    }
};
 
 
signed main(){
    int n, k;
    cin >> n >> k;
    vector<vector<int>> a(n, vector<int>(n));
    rep(i,n)rep(j,n) cin >> a[i][j];
    UnionFind ufx(n), ufy(n);
    for(int x = 0; x < n - 1; x++){
        for(int y = x+1; y < n ; y++){
            bool isOK = true;
            for(int i = 0; i < n; i++){
                if(a[i][x] + a[i][y] > k) isOK = false;
            }
            if(isOK) ufx.unite(x, y);
        }
    }
    for(int x = 0; x < n - 1; x++){
        for(int y = x+1; y < n ; y++){
            bool isOK = true;
            for(int i = 0; i < n; i++){
                if(a[x][i] + a[y][i] > k) isOK = false;
            }
            if(isOK) ufy.unite(x, y);
        }
    }
    vector<mint> fact(n+1);
    fact[0] = 1;
    rep(i,n) fact[i+1] = fact[i] * (i+1);
    mint ans = 1;
    rep(i,n){
        if(ufx.find(i) == i) ans *= fact[ufx.size(i)];
    }
    rep(i,n){
        if(ufy.find(i) == i) ans *= fact[ufy.size(i)];
    }
    cout << ans << endl;
}