#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int n, m, K, cnt[maxn], v[maxn], P, a[maxn], b[maxn], c[maxn];
long long C[maxn][maxn], res, ans;
int main() {
  scanf("%d%d%d", &n, &m, &K);
  if ((n ^ m) & 1) return puts("0"), 0;
  for (int i = 1; i <= K; i++)
    scanf("%d%d%d", &a[i], &b[i], &c[i]), c[i] = (c[i] == 1 ? 0 : 1);
  scanf("%d", &P);
  for (int i = 0; i <= 1000; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
  }
  if (n < m) {
    swap(n, m);
    for (int i = 1; i <= K; i++) swap(a[i], b[i]);
  }
  for (int i = 1; i <= n; i++) cnt[i] = m, v[i] = 1;
  for (int i = 1; i <= K; i++) cnt[a[i]]--, v[a[i]] ^= c[i];
  ans = 1;
  for (int i = 1, pd = 0; i <= n; i++) {
    if (cnt[i] == m && !pd) {
      pd = 1;
      continue;
    }
    long long res = 0;
    for (int j = v[i]; j <= cnt[i]; j += 2) (res += C[cnt[i]][j]) %= P;
    ans = (ans * res) % P;
  }
  printf("%lld\n", ans);
  return 0;
}
