#include <bits/stdc++.h>
using namespace std;
int n, m, s, ans, a[10][10];
void gao(int r, int c) {
  if (c == m) {
    int tag = -1;
    for (int i = 0; i < c; i++) tag *= a[r][i];
    if (tag < 0) return;
    if (r == n - 1)
      ans++;
    else
      gao(r + 1, 0);
    return;
  }
  if (r == n - 1) {
    int tag = -1;
    for (int i = 0; i < r; i++) tag *= a[i][c];
    a[r][c] = tag;
    gao(r, c + 1);
  } else {
    a[r][c] = +1;
    gao(r, c + 1);
    a[r][c] = -1;
    gao(r, c + 1);
  }
}
int main() {
  int n, m, s, r, c, v;
  scanf("%d%d%d", &n, &m, &s);
  if (n % 2 != m % 2) return puts("0") & 0;
  int cnt = (n - 1) * (m - 1) - s, row[1001] = {}, col[1001] = {};
  for (int i = 0; i < s; i++) {
    scanf("%d%d%d", &r, &c, &v);
    row[r] += row[r] < 0 ? -1 : 1;
    col[c] += col[c] < 0 ? -1 : 1;
    if (v < 0) row[r] *= -1, col[c] *= -1;
    if (row[r] == +m || col[c] == +n) return puts("0") & 0;
    if (row[r] == -m || col[c] == -n) cnt++;
  }
  scanf("%d", &v);
  for (s = 1; cnt--; s = s * 2 % v)
    ;
  printf("%d\n", s);
}
