#include using namespace std; //#pragma GCC optimize("Ofast") #define rep(i,n) for(ll i=0;i=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue,greater> #define all(x) (x).begin(),(x).end() #define CST(x) cout<> #define rev(x) reverse(x); using ll=long long; using vl=vector; using vvl=vector>; using pl=pair; using vpl=vector; using vvpl=vector; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; //const ll dy[9]={1,0,-1,0,1,1,-1,-1,0}; //const ll dx[9]={0,1,0,-1,1,-1,1,-1,0}; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } //0-indexed,2冪のセグメントツリー template struct SegTree { private: int n;// 葉の数 vector data;// データを格納するvector T def; // 初期値かつ単位元 function operation; // 区間クエリで使う処理 function change;// 点更新で使う処理 T find(int a, int b) { T val_left = def, val_right = def; for (a += (n - 1), b += (n - 1); a < b; a >>= 1, b >>= 1) { if ((a & 1) == 0){ val_left = operation(val_left, data[a]); } if ((b & 1) == 0){ val_right = operation(data[--b],val_right); } } return operation(val_left, val_right); } public: // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数, // _change:更新関数 SegTree(size_t _n, T _def, function _operation, function _change=[](T a,T b){return b;}) : def(_def), operation(_operation), change(_change) { n = 1; while (n < _n) { n *= 2; } data = vector(2 * n - 1, def); } void set(int i, T x) { data[i + n - 1] = x; } void build() { for (int k=n-2;k>=0;k--) data[k] = operation(data[2*k+1],data[2*k+2]); } // 場所i(0-indexed)の値をxで更新 void update(int i, T x) { i += n - 1; data[i] = change(data[i], x); while (i > 0) { i = (i - 1) / 2; data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]); } } T all_prod(){ return data[0]; } // [a, b)の区間クエリを実行 T query(int a, int b) { //return _query(a, b, 0, 0, n); return find(a,b); } // 添字でアクセス T operator[](int i) { return data[i + n - 1]; } }; ll modpow(ll a,ll n, ll mod) { a%=mod;if(a==0)return 0; ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main(){ ll h,w;cin >> h >> w; ll n;cin >> n; vector x(n),y(n),z(n); vector xx(n),yy(n),zz(n); rep(i,n){ ll a,b,c;cin >> a >> b>> c;a--;b--; x[i]=a,y[i]=b;z[i]=c; xx[i]=h-1-a,yy[i]=w-1-b; if(c==6||c==9){ c^=(6^9); } zz[i]=c; } ll base=2009; rep(i,n){ ll s=x[i]*w+y[i]; s=modpow(base,s,MOD9); z[i]=z[i]*s%MOD9; } rep(i,n){ ll s=xx[i]*w+yy[i]; s=modpow(base,s,MOD9); zz[i]=zz[i]*s%MOD9; } ll q;cin >> q; vl lx(q),rx(q),ly(q),ry(q); rep(i,q){ cin >> lx[i] >> ly[i] >> rx[i] >> ry[i]; lx[i]--;ly[i]--; } vl ans(q); vl nans(q); { SegTree st(w,0,[](ll a,ll b){return (a+b)%MOD9;},[](ll a,ll b){return (a+b)%MOD9;}); vector ques(h+1); rep(i,q){ ques[lx[i]].emplace_back(i); ques[rx[i]].emplace_back(i); } vector adds(h+1); rep(i,n){ adds[x[i]].emplace_back(i); } rep(i,h+1){ for(auto p:ques[i]){ auto f=st.query(ly[p],ry[p]); if(i==lx[p])ans[p]=(MOD9-f)%MOD9; else ans[p]=(f+ans[p])%MOD9; } for(auto p:adds[i]){ st.update(y[p],z[p]); } } } { SegTree st(w,0,[](ll a,ll b){return (a+b)%MOD9;},[](ll a,ll b){return (a+b)%MOD9;}); vector ques(h+1); rep(i,q){ ques[h-lx[i]].emplace_back(i); ques[h-rx[i]].emplace_back(i); } vector adds(h+1); rep(i,n){ adds[xx[i]].emplace_back(i); } rep(i,h+1){ for(auto p:ques[i]){ auto f=st.query(w-ry[p],w-ly[p]); if(i==h-rx[p])nans[p]=(MOD9-f)%MOD9; else nans[p]=(f+nans[p])%MOD9; } for(auto p:adds[i]){ st.update(yy[p],zz[p]); } } } rep(i,q){ ll t1=lx[i]*w+ly[i],t2=(h-rx[i])*w+(w-ry[i]); if(t1