#include <bits/stdc++.h>
using namespace std;
template <class T>
void checkmin(T &t, T x) {
  if (x < t) t = x;
}
template <class T>
void checkmax(T &t, T x) {
  if (x > t) t = x;
}
template <class T>
void _checkmin(T &t, T x) {
  if (t == -1) t = x;
  if (x < t) t = x;
}
template <class T>
void _checkmax(T &t, T x) {
  if (t == -1) t = x;
  if (x > t) t = x;
}
int a[1010], b[1010], n, m, t, x, y, z, i, j, k, p, ans;
int main() {
  cin >> n >> m >> t;
  for (i = 1; i <= max(n, m); i++) b[i] = 1;
  for (i = 1; i <= t; i++) {
    scanf("%d%d%d", &x, &y, &z);
    if (n < m) swap(x, y);
    a[x]++, b[x] *= z;
  }
  if (n < m) swap(n, m);
  cin >> p;
  ans = (n + m & 1) == 0;
  for (i = 1; i <= n; i++)
    if (!a[i] && !k)
      k = i;
    else if (a[i] == m && b[i] == 1)
      ans = 0;
    else
      for (j = 1; j <= m - a[i] - 1; j++) ans = ans * 2 % p;
  cout << ans << endl;
  return 0;
}
