#include <bits/stdc++.h>
using namespace std;
int mo, ans = 1, x, y, w, n, m, k, cnt[1010], bit[1010], now[1010];
bool rev, flag;
int main() {
  scanf("%d%d", &n, &m);
  if (n < m) {
    rev = 1;
    swap(n, m);
  }
  if ((n - m) % 2) ans = 0;
  for (int i = 1; i <= n; ++i) now[i] = 1;
  scanf("%d", &k);
  for (int i = 1; i <= k; ++i) {
    scanf("%d%d%d", &x, &y, &w);
    if (rev) swap(x, y);
    ++cnt[x];
    now[x] = w * now[x];
  }
  scanf("%d", &mo);
  bit[0] = 1;
  for (int i = 1; i <= m; ++i) bit[i] = 2ll * bit[i - 1] % mo;
  for (int i = 1; i <= n; ++i) {
    if (!cnt[i] && !flag) {
      flag = 1;
      continue;
    }
    if (cnt[i] == m && now[i] != -1) ans = 0;
    if (cnt[i] < m) ans = 1ll * ans * bit[m - cnt[i] - 1] % mo;
  }
  printf("%d\n", ans);
  return 0;
}
