MOD=998244353 class Graph(): def __init__(self,size,directed=False): self.dir=directed self.size=size self.gr=[[] for i in range(size)] self.edges=[] def add_edge(self,u,v,status={}): self.gr[u].append(self.Edge(u,v,status)) if not(self.dir): self.gr[v].append(self.Edge(v,u,status)) self.edges.append(self.Edge(u,v,status)) def node(self,v): return self.gr[v] class Edge(): def __init__(self,st,to,status): self.st=st self.to=to self.status=status def __getitem__(self,val): return self.status[val] from collections import deque def bridge(gr): pre=[-1]*gr.size low=[-1]*gr.size ret=[] for i in range(gr.size): if pre[i]!=-1: continue vert=deque([[0,i,-1,-1]]) cnt=0 while len(vert)>0: step,now,bef,ed=vert[-1] if step==1: for j in gr.node(now): if j.to!=bef: low[now]=min(low[now],low[j.to]) if ed!=-1 and pre[now]<=low[now]: ret.append(gr.node(bef)[ed]) vert.pop() continue if pre[now]!=-1: low[bef]=min(low[bef],low[now]) vert.pop() continue pre[now]=cnt low[now]=cnt cnt+=1 vert[-1][0]=1 for j,k in enumerate(gr.node(now)): if k.to==bef: continue if low[k.to]==-1: vert.append([0,k.to,now,j]) else: low[now]=min(low[now],low[k.to]) return ret N,K=map(int,input().split()) namori=Graph(N) for i in range(N): u,v=map(int,input().split()) namori.add_edge(u-1,v-1) cycle=N-len(bridge(namori)) dp=[[0,0] for i in range(cycle)] dp[0][0]=K for i in range(cycle-1): dp[i+1][0]=dp[i][1] dp[i+1][1]=(dp[i][0]*(K-1)+dp[i][1]*(K-2))%MOD print((dp[-1][1]*pow(K-1,N-cycle,MOD))%MOD)