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, d, n): mod2 = mod * 2 ret = (2 * a + d * (n-1)) % mod2 ret = ret * n % mod2 return ret // 2 for i in range(n+1): ans=a[i]+sum(s[i]-a[i],l[i],k) print(ans)