#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 10000 + 20;
long long p;
class z {
 public:
  long long key;
  z(long long s) { key = (s % p + p) % p; }
  z operator+(z s) const { return (z(key + s.key)); }
  z operator-(z s) const { return (z(key - s.key)); }
  z operator*(z s) const { return (z(key * s.key)); }
  z pow(long long s = 0) const {
    if (!s) return (z(1));
    z ans = pow(s / 2);
    ans = ans * ans;
    if (s % 2) ans = ans * (*this);
    return (ans);
  }
  z operator/(z s) const { return (operator*(s.pow(p - 2))); }
  void operator+=(z s) { *this = *this + s; }
  void operator-=(z s) { *this = *this - s; }
  void operator/=(z s) { *this = *this / s; }
  void operator*=(z s) { *this = *this * s; }
  inline friend ostream& operator<<(ostream& tout, z s) {
    return (tout << s.value());
  }
  int value() { return (key); }
  inline friend z operator+(long long s, z t) { return (t + s); }
  inline friend z operator-(long long s, z t) { return (z(0) - (t - s)); }
  inline friend z operator*(long long s, z t) { return (t * s); }
};
pair<int, int> num[MAX_N];
int n, m, k, col[MAX_N], row[MAX_N], numRow[MAX_N], numCol[MAX_N];
int main() {
  cin >> n >> m;
  if (n % 2 != m % 2) {
    cout << 0 << endl;
    return (0);
  }
  cin >> k;
  for (int i = 1; i <= n; i++) numRow[i] = 1;
  for (int i = 1; i <= m; i++) numCol[i] = 1;
  for (int i = 1; i <= k; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    numRow[a] *= c;
    numCol[b] *= c;
    row[a]++;
    col[b]++;
  }
  cin >> p;
  int count = 0;
  for (int i = 1; i <= n; i++)
    if (row[i] == m) {
      if (numRow[i] == 1) {
        cout << 0 << endl;
        return (0);
      } else
        count += 0;
    } else
      count += 1;
  for (int i = 1; i <= m; i++)
    if (col[i] == n) {
      if (numCol[i] == 1) {
        cout << 0 << endl;
        return (0);
      } else
        count += n;
    } else
      count += col[i] + 1;
  z ans = z(2).pow(n * m - count + 1);
  cout << ans.key << endl;
}
