local mod = 1000000007 local mfl = math.floor local function bmul(x, y) local x1, y1 = mfl(x / 31623), mfl(y / 31623) local x0, y0 = x - x1 * 31623, y - y1 * 31623 return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod end local function badd(x, y) return (x + y) % mod end local function bsub(x, y) return x < y and x - y + mod or x - y end local function modpow(src, pow) local res = 1 while 0 < pow do if pow % 2 == 1 then res = bmul(res, src) pow = pow - 1 end src = bmul(src, src) pow = mfl(pow / 2) end return res end local function modinv(src) return modpow(src, mod - 2) end local h, w = io.read("*n", "*n") local t = {} for i = 1, h do for j = 1, w do t[(i - 1) * w + j] = io.read("*n") end end local col_summary = {} local row_summary = {} for i = 1, w do local z = 1 for j = 1, h do local idx = (j - 1) * w + i print(i, j, idx) z = bmul(z, t[idx]) end col_summary[i] = z end local all = 1 for i = 1, h do local z = 1 for j = 1, w do local idx = (i - 1) * w + j z = bmul(z, t[idx]) end row_summary[i] = z all = bmul(all, z) end local q = io.read("*n") for iq = 1, q do local r, c = io.read("*n", "*n") local idx = (r - 1) * w + c local denom = bmul(row_summary[r], col_summary[c]) local numer = bmul(all, t[idx]) print(bmul(numer, modinv(denom))) end