1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define ROF(i, a, b) for (int i = (a); i >= (b); --i) #define all(x) x.begin(),x.end() #define CLR(i,a) memset(i,a,sizeof(i)) #define fi first #define se second #define pb push_back #define SZ(a) ((int) a.size())
using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef __int128 i128; typedef pair<ll,ll> pll; typedef array<ll,3> arr; typedef double DB; typedef pair<DB,DB> pdd; typedef pair<ll,bool> plb; constexpr ll MAXN=static_cast<ll>(2e5)+10,INF=static_cast<ll>(5e18)+3; constexpr ll mod=static_cast<ll>(1e9)+7;
ll N,M,T,K,A[MAXN]; arr xyc[MAXN]; ll dx[4]={1,0,-1,0}; ll dy[4]={0,-1,0,1};
inline bool isEff(ll x,ll y){ if(x==1 || x==N){ if(y!=1 && y!=M){ return true; } }else if(y==1 || y==M){ if(x!=1 && x!=N){ return true; } } return false; } inline ll binpow(ll x,ll k){ ll res=1; if(k<0) return -1; while(k){ if(k&1) res*=x; res%=mod; x*=x; x%=mod; k>>=1; } return res; }
inline void solve(){ cin>>N>>M>>K; ll cnt=0; vector<arr> effNode; map<pll,ll> dicNode; FOR(i,1,K){ cin>>xyc[i][0]>>xyc[i][1]>>xyc[i][2]; if(isEff(xyc[i][0],xyc[i][1])){ ++cnt; effNode.pb(xyc[i]); dicNode[(pll){xyc[i][0],xyc[i][1]}]=xyc[i][2]; } } ll res=0; #ifdef LOCAL cerr << cnt << '\n'; #endif if(cnt<2*N+2*M-8){
cout<<binpow(2,N*M-K-1)<<"\n";
}else{ for(int i=0;i<effNode.size();++i){ ll x=effNode[i][0],y=effNode[i][1],c=effNode[i][2]; if(c==1){ for(int j=0;j<4;++j){ ll ux=x+dx[j],uy=y+dy[j]; if(ux<1 || ux>N) continue; if(uy<1 || uy>M) continue; if(dicNode.find((pll){ux,uy})!=dicNode.end()){ if(dicNode[(pll){ux,uy}]==0) ++res; }else{ ++res; } } }
}
if(res%2==0){ cout<<binpow(2,N*M-K)<<"\n"; }else{ cout<<0<<"\n"; } }
}
int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>T; while(T--){ solve(); } return 0; }
|