n,k=map(int,input().split()) a=list(map(int,input().split())) p=list(map(int,input().split())) s=a.copy() mod=998244353 for i in range(n):s[p[i]]+=a[i+1] t=s.copy() for i in range(n):t[p[i]]+=s[i+1] l=[(t[i]-s[i])-(s[i]-a[i])for i in range(n+1)] def sum(a, r, n): if n == 1:return a % mod x = sum(a, r, n//2) ret = (x + pow(r, n//2, mod) * x) % mod if n & 1:ret = (a + r * ret) % mod return ret for i in range(n+1): ans=a[i] if l[i]!=0:ans+=sum(1,t[i]-s[i],k) else:ans+=(t[i]-s[i])*k%mod print(ans)