#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1000 + 10;
int n, m, k;
int col[Maxn], mul[Maxn], p, t[Maxn];
void input();
void output();
int main() {
  input();
  output();
}
void input() {
  cin >> n >> m >> k;
  for (int i = 0; i < max(m, n); i++) mul[i] = 1;
  for (int i = 0; i < max(m, n); i++) col[i] = min(m, n);
  for (int i = 0; i < k; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (m < n) swap(a, b);
    col[b]--;
    mul[b] *= c;
  }
  if (m < n) swap(m, n);
  cin >> p;
}
void output() {
  long long int ans = 1;
  if (m % 2 != n % 2) {
    cout << 0 << endl;
    return;
  }
  for (int i = 0; i < m; i++)
    if (col[i] == 0 && mul[i] == 1) {
      cout << 0 << endl;
      return;
    }
  int last;
  for (int i = 0; i < m; i++)
    if (col[i] == n) last = i;
  t[0] = 1;
  for (int i = 1; i <= n; i++) t[i] = (t[i - 1] * 2) % p;
  for (int i = 0; i < m; i++)
    if (i != last && col[i] != 0) ans = (ans * t[col[i] - 1]) % p;
  cout << ans % p << endl;
}
