CF
https://www.luogu.com.cn/problem/CF2092E
#include<bits/stdc++.h>
#define ll long long
#define db double
#define i128 __int128
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define mem(a,A) memset(a,A,sizeof(a))
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
constexpr int N=1e6+9,mod=1e9+7;
int n,m,k,a[N];
#define check (x==1 ? (y==m ? 0 : (y==1 ? 0 : 1)) : (x==n ? (y==m ? 0 : (y==1 ? 0 : 1)) : ((y==m ? 1 : (y==1 ? 1 : 0)))))
inline ll ksm(ll p){
ll res=1,b=2;
while(p){
if(p&1){
res*=b;
res%=mod;
}
p>>=1;
b*=b;
b%=mod;
}
return res;
}
void solve(){
cin>>n>>m>>k;
int x,y,c,cnt=0,sum=0;
for(int i=1;i<=k;i++){
cin>>x>>y>>c;
if(check){
cnt++;
sum+=c;
}
}
if(cnt==2*(n+m-4)){
//2^(nm-k)
if(sum&1){
cout<<"0\n";
}else{
cout<<ksm(1ll*n*m-k)<<"\n";
}
}else{
//2^(nm-k-1)
cout<<ksm(1ll*n*m-k-1)<<"\n";
}
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(0);cin.tie(0);
cout<<"\n";
int T;
cin>>T;
while(T--){
solve();
}
return 0;
}
#ifdef cat
/|、
('、.7
|、~丶
じしf_,)ノ
#endif