#include using namespace std; #include using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint998244353; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif int main() { int n, k; cin >> n >> k; vector> g(n); dsu uf(n); int s, t; rep(i,n){ int a, b; cin >> a >> b; --a;--b; if(uf.same(a,b)){ s = a; t = b; continue; } uf.merge(a,b); g[a].push_back(b); g[b].push_back(a); } queue q; q.push(s); vector dist(n, 1e9); dist[s] = 0; while(!q.empty()){ int u = q.front(); q.pop(); for(int v : g[u]){ if(dist[v]!=1e9) continue; q.push(v); dist[v] = dist[u] + 1; } } dbg(s,t); dbg(dist); vector> dp(dist[t]+1, vector(2)); dp[0][0] = 1; rep(i,dist[t]){ dp[i+1][0] += dp[i][1]; dp[i+1][1] += dp[i][0] * (k-1) + dp[i][1] * (k-2); } mint ans = dp[dist[t]][1]; ans *= k; ans *= mint(k-1).pow(n-dist[t]-1); cout << ans.val() << endl; return 0; }