1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5 #include <queue>
6 #include <vector>
7 #define max(a, b) ((a) > (b) ? (a) : (b))
8 #define min(a, b) ((a) < (b) ? (a) : (b))
9
10 inline void read(long long &x)
11 {
12 x = 0;char ch = getchar(), c = ch;
13 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();
14 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();
15 if(c == ‘-‘)x = -x;
16 }
17
18 const long long MAXN = 1000000 + 10;
19 const long long MAXM = 1000000 + 10;
20 const long long MAXK = 1000000 + 10;
21 const long long MOD = 1000000000;
22
23 long long n,m,ans1,ans2,k;
24 long long x[MAXK], y[MAXK], color[MAXK];
25 long long fa[MAXK << 1], w[MAXK << 1], ok;
26
27 //0相等,1不等
28 long long find(long long x)
29 {
30 if(x == fa[x])return x;
31 int f = find(fa[x]);
32 w[x] ^= w[fa[x]];
33 fa[x] = f;
34 return f;
35 }
36
37 long long pow(long long a, long long b)
38 {
39 long long r = 1, base = a % MOD;
40 for(;b;b >>= 1)
41 {
42 if(b & 1) r *= base, r %= MOD;
43 base *= base, base %= MOD;
44 }
45 return r % MOD;
46 }
47
48 long long solution()
49 {
50 for(register long long i = n + m + 10;i >= 1;-- i)fa[i] = i, w[i] = 0;
51 fa[n + 1] = 1;
52 register long long tmp,f1,f2,now = 0,a;
53 for(register long long i = 1;i <= k;++ i)
54 {
55 if(x[i] == 1 && y[i] == 1)continue;
56 if((x[i] & 1) || (y[i] & 1)) tmp = 0;
57 else tmp = 1;
58 tmp ^= (0 ^ color[i]);
59 f1 = find(x[i]), f2 = find(y[i] + n);
60 a = w[x[i]] ^ w[y[i] + n] ^ tmp;
61 if(f1 == f2 && a)return 0;
62 fa[f2] = f1, w[f2] = a;
63 }
64 for(register long long i = n + m;i >= 1;-- i)
65 if(fa[i] == i)++ now;
66 return pow(2, now - 1);
67 }
68
69 int main()
70 {
71 read(n), read(m), read(k);
72 for(register long long i = 1;i <= k;++ i)
73 {
74 read(x[i]), read(y[i]), read(color[i]);
75 if(x[i] == 1 && y[i] == 1)
76 if(!color[i]) ok = 1;
77 else ok = 2;
78 }
79 ans1 = solution();
80 for(register long long i = 1;i <= k;++ i) color[i] ^= 1;
81 ans2 = solution();
82 if(ok == 1) ans2 = 0;
83 if(ok == 2) ans1 = 0;
84 printf("%lld", (ans1 + ans2) % MOD);
85 return 0;
86 }