1 #include<iostream>
2 #include<cstdio>
3 #define N (200009)
4 #define MOD (1000000000)
5 using namespace std;
6
7 int n,m,k,ans,flag[2]={1,1};
8 int x[N],y[N],c[N],fa[N],g[N];
9
10 int Find(int x)
11 {
12 if (x==fa[x]) return x;
13 int f=Find(fa[x]);
14 g[x]^=g[fa[x]]; return fa[x]=f;
15 }
16
17 int Calc()
18 {
19 for (int i=1; i<=n+m; ++i) fa[i]=i,g[i]=0;
20 fa[1+n]=1;
21 for (int i=1; i<=k; ++i)
22 {
23 int fx=Find(x[i]),fy=Find(y[i]+n);
24 int temp=g[x[i]]^g[y[i]+n]^c[i];
25 if (fx!=fy) fa[fx]=fy,g[fx]=temp;
26 else if (temp) return 0;
27 }
28 int ans=-1;
29 for (int i=1; i<=n+m; ++i)
30 if (fa[i]==i)
31 {
32 if (ans==-1) ans=1;
33 else ans<<=1;
34 if (ans>=MOD) ans-=MOD;
35 }
36 return ans;
37 }
38
39 int main()
40 {
41 scanf("%d%d%d",&n,&m,&k);
42 for (int i=1; i<=k; ++i)
43 {
44 scanf("%d%d%d",&x[i],&y[i],&c[i]);
45 if (x[i]==1 && y[i]==1) flag[c[i]^1]=0,--k,--i;
46 else if (x[i]%2==0 && y[i]%2==0) c[i]^=1;
47 }
48 if (flag[0]) ans+=Calc();
49 if (flag[1])
50 {
51 for (int i=1; i<=k; ++i)
52 if (x[i]>1 && y[i]>1) c[i]^=1;
53 ans+=Calc();
54 }
55 if (ans>=MOD) ans-=MOD;
56 printf("%d\n",ans);
57 }