from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline # sys.setrecursionlimit(10**6) N,K = map(int,input().split()) e = [[] for _ in range(N)] for _ in range(N): u,v = map(int,input().split()) u -= 1 v -= 1 e[u].append(v) e[v].append(u) def namori_decomposition(e): ## cycle,root ## root : その頂点が属するcycle上の点を返す n = len(e) deg = [len(e[i]) for i in range(n)] v = deque() for i in range(n): if deg[i]==1: v.append(i) cycle = [True]*n while v: x = v.popleft() cycle[x]=False for ix in e[x]: deg[ix] -= 1 if deg[ix] == 1: v.append(ix) E = [[] for _ in range(n)] for i in range(n): for j in e[i]: if cycle[i]&cycle[j]: continue E[i].append(j) E[j].append(i) root = [-1]*n for i in range(n): if cycle[i]: v.append(i) root[i] = i while v: x = v.popleft() for ix in E[x]: if root[ix]==-1: root[ix] = i v.append(ix) return cycle,root cycle,root = namori_decomposition(e) n = sum(cycle) mod = 998244353 C = Counter(root) ans = (K-1)*(pow(K-1,n-1,mod) - pow(-1,n-1,mod))%mod #956138134 for v in C.values(): ans *= pow(K-1,v-1,mod) ans %= mod print(ans)