class SegTree: def __init__(self, op, e, n, v=None): self._n = n self._op = op self._e = e self._log = (n - 1).bit_length() self._size = 1 << self._log self._d = [self._e] * (2 * self._size) if v is not None: for i in range(self._n): self._d[self._size + i] = v[i] for i in range(self._size - 1, 0, -1): self._update(i) def set(self, p, x): assert 0 <= p < self._n p += self._size self._d[p] = x for i in range(1, self._log + 1): self._update(p >> i) def get(self, p): assert 0 <= p < self._n return self._d[p + self._size] def prod(self, l, r): assert 0 <= l <= r <= self._n sml, smr = self._e, self._e l += self._size r += self._size while l < r: if l & 1: sml = self._op(sml, self._d[l]) l += 1 if r & 1: r -= 1 smr = self._op(self._d[r], smr) l >>= 1 r >>= 1 return self._op(sml, smr) def all_prod(self): return self._d[1] def _update(self, k): self._d[k] = self._op(self._d[2 * k], self._d[2 * k + 1]) #https://qiita.com/AkariLuminous/items/32cbf5bc3ffb2f84a898からお借りしています。 N,K=list(map(int,input().split())) A=list(map(int,input().split())) B=list(map(int,input().split())) A.append(0) B.append(1) def MUL(a,b): return (a*b)%998244353 STST=SegTree(MUL,1,N,A) MOD=998244353 dp=[0]*(N+2)#Kコンボ未満 dp2=[0]*(N+2)#最大コンボがK rui=1 dp[0]=1 for i in range(1,N+2): if i>=K+1: rui=(rui-STST.prod(i-K-1,i-1)*dp[i-K-1])%MOD #K+1ノーツ目でミスするときはフルコンボ(0ノーツ目でミス)はつなげてはいけない。それ以降も同様 dp[i]=(rui*B[i-1])%MOD #iノーツ目でミス rui=(rui*A[i-1])%MOD #iノーツ目をつないだ(コンボ数+1) rui=(rui+dp[i])%MOD #0コンボ rui=0#dp[i]と同様 for i in range(N+2): if i>=K+2: rui=(rui-STST.prod(i-K-2,i-1)*dp2[i-K-2])%MOD dp2[i]=(rui*B[i-1])%MOD if i>=K+1: dp2[i]=(dp2[i]+B[i-1]*STST.prod(i-K-1,i-1)*dp[i-K-1])%MOD rui=(rui*A[i-1])%MOD rui=(rui+dp2[i])%MOD print(dp2[N+1])