#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = 307;
const int mod = 998244353;
int add(int x , int y){
x += y;
if (x >= mod) x -= mod;
return x;
}
void self_add(int &x , int y){
x = add(x , y);
}
int sub(int x , int y){
x -= y;
if (x < 0) x += mod;
return x;
}
void self_sub(int &x , int y){
x = sub(x , y);
}
int n , m;
vector<ii> E;
vector<int> adj[maxN * maxN];
int dp[maxN * maxN][maxN] , pre[maxN][maxN];
struct node{
int p[maxN] , h[maxN];
vector<int> g[maxN];
void add_edge(int u , int v , int t){
g[u].push_back(t);
g[v].push_back(t);
}
void dfs(int u , int par){
for (int id : g[u]){
int v = u ^ (E[id].fi ^ E[id].se);
if (v != par){
h[v] = h[u] + 1;
p[v] = id;
dfs(v , u);
}
}
}
void match(int x , int u , int v){
for (int i = 0 ; i < m - 1 ; i++){
self_add(pre[u][i] , dp[x][i]);
self_add(pre[v][i] , dp[x][i]);
}
while (h[u] > h[v]){
adj[p[u]].push_back(x);
u ^= (E[p[u]].fi ^ E[p[u]].se);
}
while (h[v] > h[u]){
adj[p[v]].push_back(x);
v ^= (E[p[v]].fi ^ E[p[v]].se);
}
while (u != v){
adj[p[u]].push_back(x);
adj[p[v]].push_back(x);
u ^= (E[p[u]].fi ^ E[p[u]].se);
v ^= (E[p[v]].fi ^ E[p[v]].se);
}
for (int i = 0 ; i < m - 1 ; i++){
self_sub(pre[u][i] , dp[x][i]);
self_sub(pre[v][i] , dp[x][i]);
}
}
void build(int u , int par){
for (int id : g[u]){
int v = u ^ (E[id].fi ^ E[id].se);
if (v != par){
build(v , u);
for (int i = 0 ; i < m - 1 ; i++){
self_add(pre[u][i] , pre[v][i]);
self_add(dp[p[v]][i] , pre[v][i]);
}
}
}
}
} tree[maxN];
void solve(){
cin >> n >> m;
for (int i = 0 ; i < n ; i++){
for (int j = 1 ; j < m ; j++){
int u , v;
cin >> u >> v;
tree[i].add_edge(u , v , sz(E));
E.push_back({u , v});
}
tree[i].dfs(1 , 0);
}
for (int i = 0 ; i < m - 1 ; i++) dp[i][i] = 1;
for (int i = 0 ; i < n ; i++){
for (int j = i * (m - 1) ; j < (i + 1) * (m - 1) ; j++){
tree[(i + 1) % n].match(j , E[j].fi , E[j].se);
}
tree[(i + 1) % n].build(1 , 0);
for (int u = 1 ; u <= m ; u++){
for (int t = 0 ; t < m - 1 ; t++) pre[u][t] = 0;
}
}
int ans = 0;
for (int i = (n - 1) * (m - 1) ; i < n * (m - 1) ; i++){
for (int t = 0 ; t < m - 1 ; t++){
bool ok = 0;
for (int j : adj[t]){
if (i == j) ok = 1;
}
if (ok){
self_add(ans , dp[i][t]);
}
}
}
cout << ans << "\n";
}
#define name "A"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnEodikgc29ydChhbGwodikpOyB2LnJlc2l6ZSh1bmlxdWUoYWxsKHYpKSAtIHYuYmVnaW4oKSk7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCAsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxsb25nIGxvbmcgLCBpbnQ+IGxsaTsKCmNvbnN0IGludCBtYXhOID0gMzA3Owpjb25zdCBpbnQgbW9kID0gOTk4MjQ0MzUzOwoKaW50IGFkZChpbnQgeCAsIGludCB5KXsKICAgIHggKz0geTsKICAgIGlmICh4ID49IG1vZCkgeCAtPSBtb2Q7CiAgICByZXR1cm4geDsKfQoKdm9pZCBzZWxmX2FkZChpbnQgJnggLCBpbnQgeSl7CiAgICB4ID0gYWRkKHggLCB5KTsKfQoKaW50IHN1YihpbnQgeCAsIGludCB5KXsKICAgIHggLT0geTsKICAgIGlmICh4IDwgMCkgeCArPSBtb2Q7CiAgICByZXR1cm4geDsKfQoKdm9pZCBzZWxmX3N1YihpbnQgJnggLCBpbnQgeSl7CiAgICB4ID0gc3ViKHggLCB5KTsKfQoKaW50IG4gLCBtOwp2ZWN0b3I8aWk+IEU7CnZlY3RvcjxpbnQ+IGFkalttYXhOICogbWF4Tl07CmludCBkcFttYXhOICogbWF4Tl1bbWF4Tl0gLCBwcmVbbWF4Tl1bbWF4Tl07CgpzdHJ1Y3Qgbm9kZXsKICAgIGludCBwW21heE5dICwgaFttYXhOXTsKICAgIHZlY3RvcjxpbnQ+IGdbbWF4Tl07CgogICAgdm9pZCBhZGRfZWRnZShpbnQgdSAsIGludCB2ICwgaW50IHQpewogICAgICAgIGdbdV0ucHVzaF9iYWNrKHQpOwogICAgICAgIGdbdl0ucHVzaF9iYWNrKHQpOwogICAgfQoKICAgIHZvaWQgZGZzKGludCB1ICwgaW50IHBhcil7CiAgICAgICAgZm9yIChpbnQgaWQgOiBnW3VdKXsKICAgICAgICAgICAgaW50IHYgPSB1IF4gKEVbaWRdLmZpIF4gRVtpZF0uc2UpOwogICAgICAgICAgICBpZiAodiAhPSBwYXIpewogICAgICAgICAgICAgICAgaFt2XSA9IGhbdV0gKyAxOwogICAgICAgICAgICAgICAgcFt2XSA9IGlkOwogICAgICAgICAgICAgICAgZGZzKHYgLCB1KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICB2b2lkIG1hdGNoKGludCB4ICwgaW50IHUgLCBpbnQgdil7CiAgICAgICAgZm9yIChpbnQgaSA9IDAgOyBpIDwgbSAtIDEgOyBpKyspewogICAgICAgICAgICBzZWxmX2FkZChwcmVbdV1baV0gLCBkcFt4XVtpXSk7CiAgICAgICAgICAgIHNlbGZfYWRkKHByZVt2XVtpXSAsIGRwW3hdW2ldKTsKICAgICAgICB9CiAgICAgICAgd2hpbGUgKGhbdV0gPiBoW3ZdKXsKICAgICAgICAgICAgYWRqW3BbdV1dLnB1c2hfYmFjayh4KTsKICAgICAgICAgICAgdSBePSAoRVtwW3VdXS5maSBeIEVbcFt1XV0uc2UpOwogICAgICAgIH0KICAgICAgICB3aGlsZSAoaFt2XSA+IGhbdV0pewogICAgICAgICAgICBhZGpbcFt2XV0ucHVzaF9iYWNrKHgpOwogICAgICAgICAgICB2IF49IChFW3Bbdl1dLmZpIF4gRVtwW3ZdXS5zZSk7CiAgICAgICAgfQogICAgICAgIHdoaWxlICh1ICE9IHYpewogICAgICAgICAgICBhZGpbcFt1XV0ucHVzaF9iYWNrKHgpOwogICAgICAgICAgICBhZGpbcFt2XV0ucHVzaF9iYWNrKHgpOwogICAgICAgICAgICB1IF49IChFW3BbdV1dLmZpIF4gRVtwW3VdXS5zZSk7CiAgICAgICAgICAgIHYgXj0gKEVbcFt2XV0uZmkgXiBFW3Bbdl1dLnNlKTsKICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaSA9IDAgOyBpIDwgbSAtIDEgOyBpKyspewogICAgICAgICAgICBzZWxmX3N1YihwcmVbdV1baV0gLCBkcFt4XVtpXSk7CiAgICAgICAgICAgIHNlbGZfc3ViKHByZVt2XVtpXSAsIGRwW3hdW2ldKTsKICAgICAgICB9CiAgICB9CgogICAgdm9pZCBidWlsZChpbnQgdSAsIGludCBwYXIpewogICAgICAgIGZvciAoaW50IGlkIDogZ1t1XSl7CiAgICAgICAgICAgIGludCB2ID0gdSBeIChFW2lkXS5maSBeIEVbaWRdLnNlKTsKICAgICAgICAgICAgaWYgKHYgIT0gcGFyKXsKICAgICAgICAgICAgICAgIGJ1aWxkKHYgLCB1KTsKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSAwIDsgaSA8IG0gLSAxIDsgaSsrKXsKICAgICAgICAgICAgICAgICAgICBzZWxmX2FkZChwcmVbdV1baV0gLCBwcmVbdl1baV0pOwogICAgICAgICAgICAgICAgICAgIHNlbGZfYWRkKGRwW3Bbdl1dW2ldICwgcHJlW3ZdW2ldKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfSB0cmVlW21heE5dOwoKdm9pZCBzb2x2ZSgpewogICAgY2luID4+IG4gPj4gbTsKICAgIGZvciAoaW50IGkgPSAwIDsgaSA8IG4gOyBpKyspewogICAgICAgIGZvciAoaW50IGogPSAxIDsgaiA8IG0gOyBqKyspewogICAgICAgICAgICBpbnQgdSAsIHY7CiAgICAgICAgICAgIGNpbiA+PiB1ID4+IHY7CiAgICAgICAgICAgIHRyZWVbaV0uYWRkX2VkZ2UodSAsIHYgLCBzeihFKSk7CiAgICAgICAgICAgIEUucHVzaF9iYWNrKHt1ICwgdn0pOwogICAgICAgIH0KICAgICAgICB0cmVlW2ldLmRmcygxICwgMCk7CiAgICB9CiAgICBmb3IgKGludCBpID0gMCA7IGkgPCBtIC0gMSA7IGkrKykgZHBbaV1baV0gPSAxOwogICAgZm9yIChpbnQgaSA9IDAgOyBpIDwgbiA7IGkrKyl7CiAgICAgICAgZm9yIChpbnQgaiA9IGkgKiAobSAtIDEpIDsgaiA8IChpICsgMSkgKiAobSAtIDEpIDsgaisrKXsKICAgICAgICAgICAgdHJlZVsoaSArIDEpICUgbl0ubWF0Y2goaiAsIEVbal0uZmkgLCBFW2pdLnNlKTsKICAgICAgICB9CiAgICAgICAgdHJlZVsoaSArIDEpICUgbl0uYnVpbGQoMSAsIDApOwogICAgICAgIGZvciAoaW50IHUgPSAxIDsgdSA8PSBtIDsgdSsrKXsKICAgICAgICAgICAgZm9yIChpbnQgdCA9IDAgOyB0IDwgbSAtIDEgOyB0KyspIHByZVt1XVt0XSA9IDA7CiAgICAgICAgfQogICAgfQogICAgaW50IGFucyA9IDA7CiAgICBmb3IgKGludCBpID0gKG4gLSAxKSAqIChtIC0gMSkgOyBpIDwgbiAqIChtIC0gMSkgOyBpKyspewogICAgICAgIGZvciAoaW50IHQgPSAwIDsgdCA8IG0gLSAxIDsgdCsrKXsKICAgICAgICAgICAgYm9vbCBvayA9IDA7CiAgICAgICAgICAgIGZvciAoaW50IGogOiBhZGpbdF0pewogICAgICAgICAgICAgICAgaWYgKGkgPT0gaikgb2sgPSAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChvayl7CiAgICAgICAgICAgICAgICBzZWxmX2FkZChhbnMgLCBkcFtpXVt0XSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IGFucyA8PCAiXG4iOwp9CgojZGVmaW5lIG5hbWUgIkEiCgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiAgICBpZiAoZm9wZW4obmFtZSIuSU5QIiAsICJyIikpewogICAgICAgIGZyZW9wZW4obmFtZSIuSU5QIiAsICJyIiAsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKG5hbWUiLk9VVCIgLCAidyIgLCBzdGRvdXQpOwogICAgfQogICAgaW50IHQgPSAxOyAvL2NpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgc29sdmUoKTsKICAgIHJldHVybiAwOwp9CgoK