#include <bits/stdc++.h>
using namespace std;
int n, m, k, s[1010], p, v, cnt[1010], C[1010][1010];
bool sw;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  if ((n & 1) ^ (m & 1)) {
    printf("0");
    return 0;
  }
  if (n < m) swap(n, m), sw = true;
  for (; k--;) {
    if (sw)
      scanf("%d%d%d", &p, &p, &v);
    else
      scanf("%d%d%d", &p, &v, &v);
    s[p] ^= (v == -1);
    cnt[p]++;
  }
  scanf("%d", &p);
  C[0][0] = 1;
  for (int i = 1; i <= m; 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;
  }
  long long ans = 1;
  sw = false;
  for (int i = 1; i <= n; i++) {
    int sum = 0;
    cnt[i] = m - cnt[i];
    if (!sw && cnt[i] == m) {
      sw = true;
      continue;
    }
    for (int k = (s[i] ^ 1); k <= cnt[i]; k += 2)
      sum = (sum + C[cnt[i]][k]) % p;
    ans = ans * sum % p;
  }
  printf("%d\n", (int)ans);
}
