#include using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair pii; typedef pair pll; typedef pair pdd; typedef vector vl; typedef vector> vvl; //typedef vector> Graph; const ll mod = 1e9 + 7; //const ll mod = 998244353; #define REP(i,n) for(ll i=0;i<(ll)n;i++) #define dump(x) cerr << #x << " = " << (x) << endl; #define spa << " " << #define fi first #define se second template 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; } template ostream& operator << (ostream& os, const pair v){ os << "(" << v.first << ", " << v.second << ")"; return os; } template ostream& operator << (ostream& os, const vector v){ for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os; } template ostream& operator << (ostream& os, const vector> v){ for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os; } template void debug(vector>&v,ll h,ll w){for(ll i=0;i void debug(vector&v,ll n){if(n!=0)cerr< class modint { // long long から modint を作るときは必ず正の数にしてからコンストラクタに入れること! // そうしないとバグります using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &value() noexcept { return a; } constexpr const u64 &value() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } }; using mint = modint; template constexpr T power(T x, U exp) { T ret = static_cast(1); while (exp) { if (exp % static_cast(2) == static_cast(1)) ret *= x; exp /= static_cast(2); x *= x; } return ::std::move(ret); } using vm = vector; using vvm = vector; using vvvm = vector; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll H, W; cin >> H >> W; vvl A(H, vl(W, 0)); REP(i, H){ REP(j, W){ cin >> A[i][j]; } } vvvm B(4, vvm(H, vm(W, 0))); REP(i, H)REP(j, W){ B[0][i][j] = A[i][j]; B[1][i][j] = A[H-1-i][j]; B[2][i][j] = A[i][W-1-j]; B[3][i][j] = A[H-1-i][W-1-j]; } vvvm S; auto cum2d = [&](vvm &x){ vvm cum(H+1, vm(W+1, 1)); REP(i, H)REP(j, W) cum[i+1][j+1] = cum[i+1][j] * x[i][j]; REP(i, H)REP(j, W) cum[i+1][j+1] *= cum[i][j+1]; return cum; }; REP(i, 4) S.push_back(cum2d(B[i])); ll Q; cin >> Q; while(Q--){ ll r, c; cin >> r >> c; r--, c--; mint res = 1; res *= S[0][r][c]; res *= S[1][H-1-r][c]; res *= S[2][r][W-1-c]; res *= S[3][H-1-r][W-1-c]; cout << res.value() << "\n"; } return 0; }