#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using mint = atcoder::modint998244353; ostream& operator<<(ostream& os, const mint& m){ os << m.val(); return os; } vector> transpose(vector> a){ int h = a.size(), w = a[0].size(); auto ans = vec2d(w, h, 0); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ ans[j][i] = a[i][j]; } } return ans; } using P = pair; // 横に交互を計算 mint f(vector> a){ int h = a.size(), w = a[0].size(); auto mx = vec2d(h, 2, 0); vector

vp; for(int i = 0; i < h; i++){ vector> v(2); for(int j = 0; j < w; j++){ v[j%2].push_back(a[i][j]); } for(int p = 0; p < 2; p++) mx[i][p] = *max_element(v[p].begin(), v[p].end()); } mint prod = mint(2).pow(h); vector rem(h, 2); for(int i = 0; i < h; i++){ for(int p = 0; p < 2; p++){ vp.push_back({mx[i][p], i}); } } vector inv = {mint(0), mint(1), mint(2).inv()}; mint ans = 0; sort(vp.begin(), vp.end(), greater

()); for(auto [x, i]: vp){ prod *= inv[rem[i]]; ans += x*prod; rem[i]--; prod *= rem[i]; cout << x << ':' << prod << endl; print_vector(rem); } return ans; } // 縦横に交互を計算 mint g(vector> a){ int h = a.size(), w = a[0].size(); mint ans = 0; for(int x: {0, 1}){ int mx = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ int p = (i+j)%2; p ^= x; if(p == 1) chmax(mx, a[i][j]); } } debug_value(mx) ans += mx; } return ans; } struct UnionFind { vector data; UnionFind(int size) : data(size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; template class Compress{ public: vector data; int offset; Compress(vector data_, int offset=0): offset(offset){ data = data_; sort(begin(data), end(data)); data.erase(unique(begin(data), end(data)), end(data)); }; int operator[](T x) { auto p = lower_bound(data.begin(), data.end(), x); assert(x == *p); return offset+(p-data.begin()); } T inv(int x){ return data[x-offset]; } int size(){ return data.size(); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int h, w; cin >> h >> w; auto a = vec2d(h, w, 0); vector v; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> a[i][j]; v.push_back(a[i][j]); } } auto cp = Compress(v); int m = cp.size(); vector> indices(m); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ indices[cp[a[i][j]]].push_back({i, j}); } } mint ans = 0; UnionFind uf(h+w); int rem = h+w-1; mint n_all = mint(2).pow(h+w-1); for(int x = m-1; x >= 0; x--){ for(auto [i, j]: indices[x]){ if(!uf.findSet(i, j+h)){ rem--; uf.unionSet(i, j+h); } } int diff = x == 0 ? cp.data[x] : cp.data[x]-cp.data[x-1]; ans += (n_all-mint(2).pow(rem))*diff; } cout << ans << endl; }