#include using namespace std; #define DEBUG(x) cerr<<#x<<": "< #define vl vector #define vii vector< vector > #define vll vector< vector > #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair template pair operator+(const pair &s, const pair &t) { return pair(s.first + t.first, s.second + t.second); } template pair operator-(const pair &s, const pair &t) { return pair(s.first - t.first, s.second - t.second); } template ostream& operator<<(ostream& os, pair p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(int)(n);i>0;i--) #define REP(i,a,b) for(int i=a;i bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; #define Sp(p) cout<= 0; i--) { rfact[i] = (i + 1) * rfact[i + 1] % M; } } //http://drken1215.hatenablog.com/entry/2018/06/08/210000 //n���傫��fact���v�Z�ł��Ȃ��Ƃ��̂ق��̌v�Z���@�ɂ‚��ď����Ă��� ll nCr(ll n, ll r, ll M = MOD) { if (r > n) return 0; assert(fact[2] == 2); ll ret = fact[n]; if (rfact[r] == 0) { rfact[r] = mod_inverse(fact[r], M); } ret = (ret*rfact[r]) % M; if (rfact[n - r] == 0) { rfact[n - r] = mod_inverse(fact[n - r], M); } ret = (ret*rfact[n - r]) % M; return ret; } ll nHr(ll n, ll r) { return nCr(n+r-1, r); } ll func(int a, int b, int n) { return (n + 1) * nCr(b + n + 1, b) % MOD * nCr(a + b + n + 1, a) % MOD * mod_inverse(a + b + 1) % MOD; } signed main() { fio(); set_fact(2012345); int n, m; cin >> n >> m; vii s(n, vi(m)); rep (i, n) { rep (j, m) { cin >> s[i][j]; } } ll ans = 0; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i != n and j != m) { int last = s[i][j]; ans += nCr(i + j + last - 1, i) * nCr(j + last - 1, j) % MOD; ans %= MOD; } if (i != n and j != 0) { int last = s[i][j - 1]; int start = 0; if (j != m) start = s[i][j]; ans += func(i, j - 1, last - 1) - func(i, j - 1, start - 1); ans %= MOD; // for (int k = start; k < last; k++) { // ans += nCr(i + j + k - 1, i) * nCr(j + k - 1, j - 1) % MOD; // ans %= MOD; // } } if (j != m and i != 0) { int last = s[i - 1][j]; int start = 0; if (i != n) start = s[i][j]; ans += func(i - 1, j, last - 1) - func(i - 1, j, start - 1); ans %= MOD; // for (int k = start; k < last; k++) { // ans += nCr(i + j + k - 1, i - 1) * nCr(j + k, j) % MOD; // ans %= MOD; // } } } } cout << (ans + MOD) % MOD << endl; }