#技術室奥#5 Day2 で既出です。ごめんなさい https://yukicoder.me/problems/no/1210 n, m = map(int, input().split()) x = 20 a = list(map(int, input().split())) b = list(map(int, input().split())) mod = 998244353 ax, bx = 0, 0 for i in range(n): ax ^= a[i] for i in range(m): bx ^= b[i] if ax != bx: print(0) else: print(pow(pow(2, x, mod), (n - 1) * (m - 1), mod))