#include #include #include #include using mint = atcoder::modint998244353; namespace atcoder { std::istream& operator>>(std::istream& in, mint& a) { long long e; in >> e; a = e; return in; } std::ostream& operator<<(std::ostream& out, const mint& a) { out << a.val(); return out; } } // namespace atcoder int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m; std::vector> a(n, std::vector(m)); std::map>, std::greater> pos; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { std::cin >> a[i][j]; pos[a[i][j]].emplace_back(i, j); } mint ans = 0; atcoder::dsu uf_row(n), uf_col(m); int t = n + m - 1; std::vector ng_row(n, -1), ng_col(m, -1); for (auto [v, p] : pos) { for (auto [i, j] : p) { int j2 = ng_row[i]; int i2 = ng_col[j]; if ((i2 == -1 or not uf_row.same(i, i2)) and (j2 == -1 or not uf_col.same(j, j2))) { ans += v * mint(2).pow(t - 1); --t; } if (j2 != -1) { uf_col.merge(j, j2); } else { ng_row[i] = j; } if (i2 != -1) { uf_row.merge(i, i2); } else { ng_col[j] = i; } } } std::cout << ans.val() << std::endl; }