#include<bits/stdc++.h> #define MAX_N 200000 #define MOD 1000000000 usingnamespace std; typedeflonglong lnt; template <classT> inlinevoidread(T &x){ x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, k, f[MAX_N+5], g[MAX_N+5]; int x[MAX_N+5], y[MAX_N+5], c[MAX_N+5]; intgetf(int x){return f[x] == x ? x : getf(f[x]), g[x] ^= g[f[x]], f[x] = f[f[x]];} lnt calc(int val){ lnt ret = 1; for (int i = 1; i <= k; i++) if (x[i] > 1 && y[i] > 1) c[i] ^= val; for (int i = 1; i <= n+m; i++) f[i] = i, g[i] = 0; f[n+1] = 1; for (int i = 1; i <= k; i++) if ((x[i]^1) || (y[i]^1)) { int u = getf(x[i]), v = getf(y[i]+n), w = g[x[i]]^g[y[i]+n]^c[i]; if (u^v) f[v] = u, g[v] = w; elseif (w) return0LL; } for (int i = 1, t = 0; i <= n+m; i++) if (getf(i) == i) {if (t) (ret *= 2LL) %= MOD; else t = 1;} return ret; } intmain(){ read(n), read(m), read(k); bool f0 = true, f1 = true; lnt ans = 0; for (int i = 1; i <= k; i++) { read(x[i]), read(y[i]), read(c[i]); if (!(x[i]%2) && !(y[i]%2)) c[i] ^= 1; if (x[i] == 1 && y[i] == 1) c[i] ? f0 = false : f1 = false; } if (f0) (ans += calc(0)) %= MOD; if (f1) (ans += calc(1)) %= MOD; returnprintf("%lld\n", ans), 0; }