/* -*- coding: utf-8 -*- * * 1141.cc: No.1141 田グリッド - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_H = 100000; const int MAX_W = 100000; const int MOD = 1000000007; /* typedef */ typedef long long ll; typedef vector vi; typedef vector vvi; /* global variables */ /* subroutines */ void pmat(int h, int w, vvi &v) { for (int y = 0; y <= h; y++) for (int x = 0; x <= w; x++) { printf("%d ", v[y][x]); putchar(x < w ? ' ' : '\n'); } } /* main */ int main() { int h, w; scanf("%d%d", &h, &w); vvi as(h, vi(w, 1)); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) scanf("%d", &as[y][x]); vvi m00(h + 1, vi(w + 1, 1)); vvi m01(h + 1, vi(w + 1, 1)); vvi m10(h + 1, vi(w + 1, 1)); vvi m11(h + 1, vi(w + 1, 1)); for (int y0 = 0, y1 = h - 1; y0 < h; y0++, y1--) { int p00 = 1, p01 = 1, p10 = 1, p11 = 1; for (int x0 = 0, x1 = w - 1; x0 < w; x0++, x1--) { p00 = (ll)p00 * as[y0][x0] % MOD; p01 = (ll)p01 * as[y0][x1] % MOD; p10 = (ll)p10 * as[y1][x0] % MOD; p11 = (ll)p11 * as[y1][x1] % MOD; m00[y0 + 1][x0 + 1] = (ll)m00[y0][x0 + 1] * p00 % MOD; m01[y0 + 1][x1] = (ll)m01[y0][x1] * p01 % MOD; m10[y1][x0 + 1] = (ll)m10[y1 + 1][x0 + 1] * p10 % MOD; m11[y1][x1] = (ll)m11[y1 + 1][x1] * p11 % MOD; } } //pmat(h, w, m00); pmat(h, w, m01); pmat(h, w, m10); pmat(h, w, m11); int q; scanf("%d", &q); while (q--) { int r, c; scanf("%d%d", &r, &c); r--, c--; int p = (ll)m00[r][c] * m01[r][c + 1] % MOD * m10[r + 1][c] % MOD * m11[r + 1][c + 1] % MOD; printf("%d\n", p); } return 0; }