#include <bits/stdc++.h>
using namespace std;
const int M = 1000 + 10;
int number[M];
int flag[M];
int fastexp(long long int a, int n, long long int p) {
  long long int ans = 1;
  while (n != 0) {
    if (n % 2 == 1) {
      ans = (ans * a) % p;
    }
    a = (a * a) % p;
    n = n / 2;
  }
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  if ((n + m) % 2 == 1) {
    cout << "0" << endl;
  } else {
    int swap = 0;
    if (n > m) {
      int tmp = n;
      n = m;
      m = tmp;
      swap = 1;
    }
    for (int i = 1; i <= m; i++) {
      number[i] = n;
      flag[i] = 1;
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      if (swap == 1) {
        int tmp = a;
        a = b;
        b = tmp;
      }
      number[b]--;
      if (c == -1) {
        flag[b] = -flag[b];
      }
    }
    long long int p;
    cin >> p;
    bool used = false;
    bool ok = true;
    long long int ans = 1;
    for (int i = 1; i <= m; i++) {
      if (number[i] == 0) {
        if (flag[i] == 1) {
          cout << "0" << endl;
          ok = false;
          break;
        }
      }
      if (number[i] == n) {
        if (used == false) {
          used = true;
        } else {
          int ans1 = fastexp(2, number[i] - 1, p);
          ans = (ans * ans1) % p;
        }
      }
      if (number[i] != 0 && number[i] != n) {
        int ans1 = fastexp(2, number[i] - 1, p);
        ans = (ans * ans1) % p;
      }
    }
    if (ok == true) {
      cout << ans << endl;
    }
  }
  return 0;
}
