#include <bits/stdc++.h>
using namespace std;
int ans, n, m, k;
int a[1010], b[1010];
int x, y, c, p;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  if ((n + m) % 2 == 1) return puts("0"), 0;
  for (int i = 1; i <= max(n, m); ++i) b[i] = 1;
  for (int i = 1; i <= k; ++i) {
    scanf("%d%d%d", &x, &y, &c);
    if (n < m) swap(x, y);
    a[x]++;
    b[x] *= c;
  }
  if (n < m) swap(n, m);
  cin >> p;
  ans = 1;
  bool flag = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == m && b[i] == 1) return puts("0"), 0;
    if (!a[i] && !flag)
      flag = 1;
    else
      for (int j = 1; j < m - a[i]; j++) ans = ans * 2 % p;
  }
  cout << ans;
  return 0;
}
