def main(): from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum from heapq import heapify, heappop, heappush from bisect import bisect_left, bisect_right from copy import deepcopy import copy import random from collections import deque,Counter,defaultdict from itertools import permutations,combinations from decimal import Decimal,ROUND_HALF_UP #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP) from functools import lru_cache, reduce #@lru_cache(maxsize=None) from operator import add,sub,mul,xor,and_,or_,itemgetter INF = 10**18 mod1 = 10**9+7 mod2 = 998244353 #DecimalならPython #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!! def modinv(a, b): p = b x, y, u, v = 1, 0, 0, 1 while b: k = a // b x -= k * u y -= k * v x, u = u, x y, v = v, y a, b = b, a % b x %= p if x < 0: x += p return x ''' または、か ''' H,W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] acm1 = [[1]*(W) for _ in range(H)] acm2 = [[1]*(W) for _ in range(H)] sm = 1 for i in range(H): for j in range(W): sm *= A[i][j] acm1[i][j] = A[i][j] acm2[i][j] = A[i][j] for i in range(H): for j in range(W-1): acm2[i][j+1] *= acm2[i][j] acm2[i][j+1] %= mod1 for j in range(W): for i in range(H-1): acm1[i+1][j] *= acm1[i][j] acm1[i+1][j] %= mod1 gyou = [0]*(H) for i in range(H): gyou[i] = modinv(acm2[i][W-1],mod1) retu = [0]*W for i in range(W): retu[i] = modinv(acm1[H-1][i],mod1) Q = int(input()) for _ in range(Q): r,c = map(int, input().split()) print(sm*gyou[r-1]%mod1*retu[c-1]%mod1*A[r-1][c-1]%mod1) if __name__ == '__main__': main()