#include <bits/stdc++.h>
using namespace std;
#define fi			first
#define se			second
#define pb			push_back
#define fz(i,a,b)	for(int i=a;i<=b;i++)
#define fd(i,a,b)	for(int i=a;i>=b;i--)
#define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define all(a)		a.begin(),a.end()
#define fill0(a)	memset(a,0,sizeof(a))
#define fill1(a)	memset(a,-1,sizeof(a))
#define fillbig(a)	memset(a,0x3f,sizeof(a))
#define y1			y10101010101
#define y0			y01010101010
typedef pair<int,int> pii;
typedef long long ll;
const ll MOD=998244353;
int n,k,a[55][55];
ll f[55];
vector<int> gc[55],gr[55];
bool vc[55],vr[55];
int ac=0,ar=0;
inline void dc(int x){
	if(vc[x]) return;
	ac++;vc[x]=1;
	foreach(it,gc[x]) dc(*it);
}
inline void dr(int x){
	if(vr[x]) return;
	ar++;vr[x]=1;
	foreach(it,gr[x]) dr(*it);
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)
		scanf("%d",&a[i][j]);
	f[0]=1;for(int i=1;i<=n;i++) f[i]=f[i-1]*i%MOD;
	for(int x=1;x<=n;x++) for(int y=1;y<x;y++){
		bool flag=1;
		for(int i=1;i<=n;i++) flag&=(a[i][x]+a[i][y]<=k);
		if(flag) gc[x].pb(y),gc[y].pb(x);
		flag=1;
		for(int i=1;i<=n;i++) flag&=(a[x][i]+a[y][i]<=k);
		if(flag) gr[x].pb(y),gr[y].pb(x);
	}
	ll ans=1;
	for(int i=1;i<=n;i++) if(!vc[i]) ac=0,dc(i),ans=ans*f[ac]%MOD;
	for(int i=1;i<=n;i++) if(!vr[i]) ar=0,dr(i),ans=ans*f[ar]%MOD;
	printf("%lld\n",ans);
	return 0;
}
#include<bits/stdc++.h>
#define int long long 
#define ri register signed
#define rd(x) x=read()
using namespace std;
const int N=1e7+5;
const int M=1e6+5;
const int maxn=5e6+5;
const int mod=998244353;
const int inf=0x3f3f3f3f3f3f3f3f;
inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline int ksm(int x,int y=mod-2,int z=mod){int ret=1;while (y){if (y&1) ret=(ret*x)%z;x=(x*x)%z;y>>=1;}return ret;}
int inv[N],fac[N],ifc[N]; 
void Init(int n)
{
    inv[1]=1;for (ri i=2;i<=n;i++)inv[i]=inv[mod%i]*(mod-mod/i)%mod;
    fac[0]=1;for (ri i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
    ifc[0]=1;for (ri i=1;i<=n;i++) ifc[i]=ifc[i-1]*inv[i]%mod;
}
int C(int n,int m){if (m>n || m<0) return 0;return fac[n]*ifc[m]%mod*ifc[n-m]%mod;}
int n,m,ans;
int x[N],y[N],z[N];
int get(int S,int k)
{
	int ret=0;
	for (int i=0;i<k;i++) ret+=(S>>i)&1;
	return ret;
}
int mp[N];
int dfs(int S)
{
	if (S==(1<<n)-1) return 1;
	if (mp[S]!=-1) return mp[S];
	int ret=0;
	int num=get(S,n);
	for (int i=0;i<n;i++) if ((S>>i)&1^1)
	{
		int T=S^(1<<i);
		bool flag=1;
		for (int j=1;j<=m;j++)
			if (y[j]==num+1)
				if (get(T,x[j])>z[j]) flag=0;
		if (flag) ret+=dfs(T);
	}
	return mp[S]=ret;
}
signed main()
{
	rd(n);rd(m);
	for (int i=1;i<=m;i++) rd(x[i]),rd(y[i]),rd(z[i]);
	memset(mp,-1,sizeof(mp));
	cout<<dfs(0)<<endl;
}
