#include // #include #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define INF 2000000000000000000 #define ll long long #define ull unsigned long long #define ld long double #define pll pair using namespace std; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b par; vector tree_rank; vector num; union_find(ll n) { par = vector(n); num = vector(n, 1); tree_rank = vector(n); for (ll i = 0; i < n; ++i) { par.at(i) = i; tree_rank.at(i) = 0; } } ll find(ll x) { if (par.at(x) == x) { return x; } else { return par.at(x) = find(par.at(x)); } } ll get_num(ll x) { return num.at(find(x)); } void unite(ll x, ll y) { x = find(x); y = find(y); if (x == y) { return; } if (tree_rank.at(x) < tree_rank.at(y)) { par.at(x) = y; num.at(y) = num.at(x) + num.at(y); } else { par.at(y) = x; num.at(x) = num.at(x) + num.at(y); if (tree_rank.at(x) == tree_rank.at(y)) { tree_rank.at(x)++; } } } bool same(ll x, ll y) { return find(x) == find(y); } }; // ======================================= ll mod = 998244353; //=============modinv============================ ll modinv(ll a, ll m = mod) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } //================================================= int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll H, W; cin >> H >> W; vector power(H + W + 1, 1); for (ll i = 0; i < H + W; ++i) { power.at(i + 1) = power.at(i) * 2 % mod; } vector> A(H, vector(W)); vector> num; for (ll i = 0; i < H; ++i) { for (ll j = 0; j < W; ++j) { cin >> A.at(i).at(j); num.push_back({A.at(i).at(j), i, j}); } } sort(rrng(num)); ll r_num = W, c_num = H; vector row(W, false); vector col(H, false); ll ans = 0; vector> already(H); union_find uf(W); for (ll i = 0; i < num.size(); ++i) { vector n = num.at(i); ll h = n.at(1), w = n.at(2); if (already.at(h).size() != 0) { ll nw = already.at(h).at((ll)already.at(h).size() - 1); if (!uf.same(w, nw)) { r_num -= 1; } else { continue; } uf.unite(w, nw); } already.at(h).push_back(w); if (!col.at(h)) { col.at(h) = true; c_num -= 1; } ans += (n.at(0) * power.at(r_num) % mod) * power.at(c_num) % mod; ans %= mod; } ans *= modinv(2); ans %= mod; cout << ans << "\n"; } //0011 //1100 //1100 //0011 //0101 //1010 //1010 //0101