#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint998244353; template vector> ranked_zeta(const vector& f) { int N = f.size(); vector> fr(N); for (int i = 0; i < N; i++) fr[i][__builtin_popcount(i)] = f[i]; for (int k = 1; k < N; k <<= 1) { for (int i = k; i < N; i = (i + 1) | k) { for (int p = 0; p < sz; p++) { fr[i][p] += fr[i ^ k][p]; } } } return fr; } template vector ranked_mobius(vector> fr) { int N = fr.size(); for (int k = 1; k < N; k <<= 1) { for (int i = k; i < N; i = (i + 1) | k) { for (int p = 0; p < sz; p++) { fr[i][p] -= fr[i ^ k][p]; } } } vector f(N); for (int i = 0; i < N; i++) f[i] = fr[i][__builtin_popcount(i)]; return f; } template vector subset_convolution(const vector& f, const vector& g) { assert(size(f) == size(g)); const int N = size(f), n = __builtin_ctz(size(f)); assert(N == 1 << n); assert(sz >= n); auto fr = ranked_zeta(f); auto gr = ranked_zeta(g); for (int i = 0; i < N; i++) { for (int p = sz; p >= 0; p--) { for (int q = sz - p; q > 0; q--) { fr[i][p + q] += fr[i][p] * gr[i][q]; } fr[i][p] *= gr[i][0]; } } return ranked_mobius(move(fr)); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; if (n < m) { VVI a(m, VI(n)); rep(i, n) rep(j, m) cin >> a[j][i]; swap(n, m); set cand; rep(j, m) cand.insert(a[0][j]); mint ans; for (int x : cand) { vector dp(1 << m), ndp; int s0 = 0; rep(j, m) s0 |= (a[0][j] == x) << j; dp[s0] = 1; rep(i, n) { ndp.assign(1 << m, 0); VI nbit(m); rep(j, m) nbit[j] = (i + 1 < n && a[i+1][j] == x) << j; rep(s, 1 << m) if (mint v = dp[s]; v.val()) { rep(j, m) if (s >> j & 1) { ndp[(s ^ (1 << j)) | nbit[j]] += v; } } swap(dp, ndp); } ans += accumulate(all(dp), mint()); } cout << ans.val() << '\n'; return 0; } else { VVI a(n, VI(m)); rep(i, n) rep(j, m) cin >> a[i][j]; set cand; rep(i, n) cand.insert(a[i][0]); mint ans; for (int x : cand) { vector dp(1 << m), ndp; dp[0] = 1; rep(i, n) if (a[i][0] == x) { ndp.assign(1 << m, mint()); ndp[0] = 1; rep(s, 1 << m) if (s) { int b = m - 1; while (~s >> b & 1) b--; b--; bool ok = true; for (; b >= 0; b--) if (s >> b & 1) { if (a[i][b+1] != x) { ok = false; break; } } if (ok) ndp[s] = 1; } dp = subset_convolution<17>(dp, ndp); dp.resize(1 << m); } ans += dp.back(); } cout << ans.val() << '\n'; } }