class cumsum2D: def __init__(self, data): self.N = len(data) self.data=data self.M= len(data[0]) self.cum = [[1] * (self.M + 1) for _ in range(self.N + 1)] for i in range(self.N): for j in range(self.M): self.cum[i + 1][j + 1] = self.cum[i][j + 1] * self.cum[i + 1][j] %MOD * pow(self.cum[i][j],MOD-2,MOD) %MOD * data[i][j] %MOD def get(self, h1, w1, h2, w2): """ 長方形の領域 [h1,h2)*[w1,w2) の範囲内にインプットされた数字の和を取る """ if h1>h2 or w1>w2: return 0 return self.cum[h2][w2] * pow(self.cum[h1][w2],MOD-2,MOD) %MOD * pow(self.cum[h2][w1],MOD-2,MOD) %MOD * self.cum[h1][w1] %MOD def __str__(self): res=["input #########################"] for line in self.data: res.append(str(line)) res.append("cumsum #########################") for line in self.cum: res.append(str(line)) return "\n".join(res) def example(): global input example = iter( """ 3 4 6 8 6 8 3 4 2 9 2 8 6 3 3 3 2 1 2 1 3 """ .strip().split("\n")) input = lambda: next(example) ######################################################################################################## import sys input = sys.stdin.readline # example() MOD=10**9+7 H, W = map(int, input().split()) data = [] for h in range(H): data.append(list(map(int, input().split()))) cum = cumsum2D(data) Q=int(input()) for _ in range(Q): h,w=map(int, input().split()) h,w=h-1,w-1 print(cum.get(0,0,h,w)*cum.get(h+1,w+1,H,W)%MOD*cum.get(h+1,0,H,w)%MOD*cum.get(0,w+1,h,W)%MOD)