#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#include<map>
#include<iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
#define input ios_base::sync_with_stdio(0);cin.tie(0);

const double PI  = acos(-1.0);
using namespace std;
//typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//typedef complex<double> point;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//char rv[4]={'D','R','L','U'};
const double EPS = 1e-9;
const int N = 50 + 9;
ll mod=998244353 , n,k,a[N][N],vis[N],cnt,ans=1,f[N];
vector<int> adj[N];
void dfs(int u)
{
    vis[u] = 1;
    cnt++;
    for(auto v:adj[u])
    {
        if(!vis[v])dfs(v);
    }
}

 int main()
{
//freopen("calc.in","r",stdin);
//freopen("calc.out","w",stdout);
//__builtin_popcount()
input
        f[0] = 1;
        forr(i,1,50)
        {
            f[i] = ((ll)i * f[i-1]) % mod;
            //cout<<f[i]<<endl;
        }
        cin>>n>>k;

        forr(i,1,n)
        forr(j,1,n)
        cin>>a[i][j];


        forr(i,1,n)
        {
            forr(j,i+1,n)
            {
                int c=0;
                forr(u,1,n)
                {
                    if(a[u][i] + a[u][j] <= k)c++;
                }
                if(c == n)
                {
                    adj[i].push_back(j);
                    adj[j].push_back(i);
                }
            }
        }

        forr(i,1,n)
        {
            if(!vis[i])
            {
                cnt=0;
                dfs(i);
                ans *= f[cnt];
                ans %= mod;
            }
        }

        forr(i,1,n)adj[i].clear();

        forr(i,1,n)
        {
            forr(j,i+1,n)
            {
                int c=0;
                forr(u,1,n)
                {
                    if(a[i][u] + a[j][u] <= k)c++;
                }
                if(c == n)
                {
                    adj[i].push_back(j);
                    adj[j].push_back(i);
                }
            }
        }

        memset(vis,0,sizeof vis);

        forr(i,1,n)
        {
            if(!vis[i])
            {
                cnt=0;
                dfs(i);
                ans *= f[cnt];
                ans %= mod;
            }
        }


        cout<<ans<<endl;







return 0;
}

#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define PRINT(a) printf("%.15Lf\n",a);
#define YESNO(a) cout << (a ? "YES" : "NO") << endl
#define YesNo(a) cout << (a ? "Yes" : "No") << endl
#define yesno(a) cout << (a ? "yes" : "no") << endl
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;

vector<bool>F(100010, false);
int N;
VI C;
vector<VI>G;
VI ans;

void dfs(int now, int pa) {
	bool ok = 0;
	if (!F[C[now]]) {
		ans.push_back(now + 1);
		F[C[now]] = 1;
		ok = 1;
	}
	fore(to, G[now]) {
		if (to == pa)continue;
		dfs(to, now);
	}
	if (ok)F[C[now]] = 0;
	return;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N;
	C.resize(N);
	G.resize(N);
	REP(i, N)cin >> C[i];
	REP(i, N - 1) {
		int a, b; cin >> a >> b;
		a--, b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	

	dfs(0,-1);

	SORT(ans);
	fore(i, ans)cout << i << '\n';
	return 0;
}

