class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x mod = 998244353 h,w = map(int,input().split()) a = [list(map(int,input().split())) for i in range(h)] v = [] for i in range(h): for j in range(w): v.append((a[i][j] << 30) + (i << 15) + j) v.sort() mask = (1 << 15) - 1 uf = UnionFind(w + h) cnt = w + h - 2 ans = 0 for ty in v[::-1]: j = ty & mask i = (ty >> 15) & mask val = ty >> 30 if uf.find(j) == uf.find(i+w): continue uf.union(j,i+w) ans += pow(2, cnt, mod) * val ans %= mod cnt -= 1 print(ans)