from types import GeneratorType from collections import deque def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc N, K = map(int, input().split()) G = [[] for _ in range(N)] for _ in range(N): u, v = map(int, input().split()) G[u-1].append(v-1) G[v-1].append(u-1) MOD = 998244353 @bootstrap def dfs(n, pre): visited[n] = True stack.append(n) for v in G[n]: if not visited[v]: yield dfs(v, n) elif v != pre and not finished[v]: for s in stack: cycle.append(s) cycle.append(v) finished[n] = True stack.pop() yield stack = [] cycle = [] visited = [False]*N finished = [False]*N dfs(0, -1) c = cycle.pop() cycle = cycle[::-1] while cycle[-1] != c: cycle.pop() dp = [[0]*2 for _ in range(len(cycle))] dp[0][0] = 1 for i in range(len(cycle)-1): dp[i+1][1] += dp[i][0]*(K-1) dp[i+1][1] %= MOD dp[i+1][0] += dp[i][1] dp[i+1][0] %= MOD dp[i+1][1] += dp[i][1]*(K-2) dp[i+1][1] %= MOD ans = dp[-1][1]*K%MOD S = set(cycle) visited = [False]*N for c in cycle: visited[c] = True for c in cycle: for v in G[c]: if v not in S: que = deque() que.append(v) visited[v] = True cnt = 1 while que: n = que.popleft() for v in G[n]: if not visited[v]: visited[v] = True que.append(v) cnt += 1 ans *= pow(K-1, cnt, MOD) ans %= MOD print(ans)