n,m = map(int,input().split()) A = list(map(int,input().split())) B = list(map(int,input().split())) ca,cb = 0,0 for a in A: ca ^= a for b in B: cb ^= b if ca != cb: print(0) exit() mod = 998244353 ans = pow(1<<20,(n-1)*(m-1),mod) print(ans)