n,k = map(int,input().split()) e = [[] for i in range(n)] mod = 998244353 count = [0]*n for _ in range(n): u,v = [int(x)-1 for x in input().split()] e[u].append(v) e[v].append(u) count[u] += 1 count[v] += 1 vis = [0]*n q = [] for i in range(n): if count[i] == 1: q.append(i) while q: now = q.pop() vis[now] = 1 for nex in e[now]: count[nex] -= 1 if count[nex] == 1: q.append(nex) assert sum(vis) < n size = n - sum(vis) dp = [k,0] for _ in range(size-1): dp = [dp[1],dp[0]*(k-1)+dp[1]*(k-2)] dp[0] %= mod dp[1] %= mod ans = dp[1] * pow(k-1,sum(vis),mod) ans %= mod print(ans)