# 分析：

#include <iostream>
#include <cstring>
#include <cstdio>

#define MX 2000002
#define MD 1000000000

using namespace std;

typedef long long ll;

int fa[MX],dis[MX];
int must[MX],have[MX];
int n,m,k;
int px[MX],py[MX],pn[MX];
int vis[MX];

ll ksm(ll x,int t)
{
ll ans=1;
while(t)
{
if(t&1)ans=ans*x%MD;
x=x*x%MD;
t>>=1;
}
return ans;
}

int findfa(int x)
{
if(fa[x]==x)return x;
else
{
findfa(fa[x]);
dis[x]+=dis[fa[x]];
fa[x]=fa[fa[x]];
return fa[x];
}
}

ll work(int top)
{
int f1,f2,x,y,del,ans=0;
for(int i=1;i<=n+m;i++)fa[i]=i,dis[i]=0,vis[i]=0,must[i]=0,have[i]=0;
for(int i=1;i<=k;i++)
{
if(px[i]==1||py[i]==1)continue;
f1=findfa(x=px[i]);
f2=findfa(y=(py[i]+n));
del=(top+pn[i])&1;
if(f1==f2)
{
if(((x&1)||(y&1)))
{
if((dis[x]+del)%2!=dis[y]%2)return 0;
}
else
{
if((dis[x]+del)%2==dis[y]%2)return 0;
}
}
else
{
fa[f1]=f2;
dis[f1]=((dis[x]%2!=dis[y]%2)+del+((x&1)||(y&1))+1)%2;
}
}
fa[1]=n+1;
have[findfa(1)]=1;
must[fa[1]]=top;
for(int i=1;i<=k;i++)
{
x=px[i];
y=n+py[i];
if(px[i]==1)
{
if(have[findfa(y)]==0)have[fa[y]]=1,must[fa[y]]=(pn[i]+dis[y])%2;
else if(must[fa[y]]!=(pn[i]+dis[y])%2)return 0;
}
if(py[i]==1)
{
if(have[findfa(x)]==0)have[fa[x]]=1,must[fa[x]]=(pn[i]+dis[x])%2;
else if(must[fa[x]]!=(pn[i]+dis[x])%2)return 0;
}
}
for(int i=1;i<=n+m;i++)
if(!vis[findfa(i)]&&!have[findfa(i)])
vis[fa[i]]=1,ans++;
return ksm(2,ans);
}

int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=k;i++)scanf("%d%d%d",&px[i],&py[i],&pn[i]);
cout<<(work(0)+work(1))%MD<<endl;
return 0;
}