#line 1 "Main.cpp" #include #include #include #include #include #line 4 "nachia\\set\\dsu.hpp" namespace nachia { struct Dsu{ private: int N; std::vector P; std::vector H; public: Dsu() : N(0) {} Dsu(int n) : N(n), P(n, -1), H(n) { for(int i=0; i= 0){ P[u] = P[v]; u = v; v = P[v]; } return P[u]; } int append(){ int n = P.size(); P.push_back(-1); H.push_back(n); return n; } int label(int u){ return H[leader(u)]; } int operator[](int u){ return H[leader(u)]; } void merge(int u, int v, int newLabel){ if(newLabel < 0) newLabel = u; u = leader(u); v = leader(v); if(u == v){ H[u] = newLabel; return; } N--; if(-P[u] < -P[v]) std::swap(u, v); P[u] += P[v]; H[P[v] = u] = newLabel; } int merge(int u, int v){ merge(u, v, u); return u; } int count(){ return N; } int size(int u){ return -P[leader(u)]; } bool same(int u, int v){ return leader(u) == leader(v); } }; } // namespace nachia #line 7 "Main.cpp" using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; int main(){ int H, W; cin >> H >> W; Modint ans = 0; nachia::Dsu dsu(H+W); vector> A(H, vector(W)); rep(y,H) rep(x,W) cin >> A[y][x]; vector> Q(H*W); rep(y,H) rep(x,W) Q[y*W+x] = {y,x}; sort(Q.begin(), Q.end(), [&](auto l, auto r){ return A[l.first][l.second] > A[r.first][r.second]; }); for(auto [y,x] : Q) if(!dsu.same(x,W+y)){ ans += Modint(2).pow(dsu.count() - 2) * A[y][x]; dsu.merge(x,W+y); } cout << ans.val() << endl; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;