#include <bits/stdc++.h>
using namespace std;
const int N = 1011;
int n, m, k, p;
void inc(int &x, int y) { x = x + y >= p ? x + y - p : x + y; }
int gao(int *a, int n) {
  static int d[N];
  int i, m;
  for (i = 1; i <= n; ++i) d[i] = a[i];
  sort(d + 1, d + n + 1);
  m = unique(d + 1, d + n + 1) - d;
  for (i = 1; i <= n; ++i) a[i] = lower_bound(d + 1, d + m, a[i]) - d;
  return m - 1;
}
int a[N], b[N], c[N];
int f[N][2];
int e[N][N];
int main() {
  cin >> n >> m >> k;
  if (n % 2 != m % 2) {
    puts("0");
    return 0;
  }
  int i;
  for (i = 1; i <= k; ++i) cin >> a[i] >> b[i] >> c[i], c[i] = c[i] < 0 ? 1 : 0;
  cin >> p;
  int r1 = gao(a, k), r2 = gao(b, k);
  if (r1 >= n) {
    swap(n, m);
    swap(a, b);
  }
  for (i = 1; i <= k; ++i) e[a[i]][b[i]] = c[i] + 1;
  int j, x, y, z, an = 1;
  for (i = 1; i < n; ++i) {
    memset(f, 0, sizeof f);
    f[0][0] = 1;
    for (j = 1; j <= m; ++j) {
      x = e[i][j];
      y = f[j - 1][0];
      inc(y, f[j - 1][1]);
      if (!x) {
        f[j][0] = y;
        f[j][1] = y;
      } else {
        --x;
        f[j][0] = f[j - 1][x];
        f[j][1] = f[j - 1][x ^ 1];
      }
    }
    an = 1LL * an * f[m][1] % p;
  }
  cout << an;
}
