#include <bits/stdc++.h>
using namespace std;
int n, m, fl, fg, rs = 1, M, K, p[1111], c[1111];
int main() {
  scanf("%d%d", &n, &m);
  if (n < m) swap(n, m), fl = 1;
  memset(p, 1, sizeof p);
  scanf("%d", &K);
  for (int i = 1, x, y, v; i <= K; ++i) {
    scanf("%d%d%d", &x, &y, &v);
    if (fl) swap(x, y);
    p[x] *= v;
    c[x]++;
  }
  scanf("%d", &M);
  for (int i = 1; i <= n; ++i) {
    if (!fg && !c[i]) {
      fg = 1;
    } else if (c[i] == m && p[i] > 0)
      rs = 0;
    else
      for (int j = 1; j <= m - c[i] - 1; ++j) (rs *= 2) %= M;
  }
  if ((n + m) & 1) rs = 0;
  cout << rs << endl;
}
