#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int add() { return 0; }
template <typename... M>
int add(int a, M... b) {
  int x = add(b...);
  return a + x - mod * (a + x >= mod);
}
int mul() { return 1; }
template <typename... M>
int mul(int a, M... b) {
  return int(1ll * a * mul(b...) % mod);
}
int pwr(int a, long long x) {
  return (x ? mul(pwr(mul(a, a), x >> 1), (x & 1 ? a : 1)) : 1);
}
int dvd(int a, int b) { return mul(a, pwr(b, mod - 2)); }
int sub(int a, int b) { return a - b + mod * (a < b); }
void inc(int& a, int b) { a = add(a, b); }
void dec(int& a, int b) { a = sub(a, b); }
vector<int> fact = {1}, invf = {1};
int C(int n, int k) {
  if (n < k or 0 > k) return 0;
  while (fact.size() <= n) {
    fact.push_back(mul(fact.back(), fact.size()));
    invf.push_back(dvd(1, fact.back()));
  }
  return mul(fact[n], invf[k], invf[n - k]);
}
const int N = 1e6 + 5;
int cntr[N][2];
int cntc[N][2];
int tot[2];
map<pair<int, int>, int> a;
bool badr(int i) { return cntr[i][0] and cntr[i][1]; }
bool detr(int i) { return cntr[i][0] or cntr[i][1]; }
bool badc(int i) { return cntc[i][0] and cntc[i][1]; }
bool detc(int i) { return cntc[i][0] or cntc[i][1]; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  int dr = 0, dc = 0, br = 0, bc = 0;
  while (q--) {
    int x, y, t;
    cin >> x >> y >> t;
    int ans = 0;
    if (a.count(make_pair(x, y))) {
      int cur = a[make_pair(x, y)];
      a.erase(make_pair(x, y));
      bool wasbr = badr(x);
      bool wasdr = detr(x);
      --cntr[x][(y & 1) ^ cur];
      if (wasbr and !badr(x)) {
        --br;
      }
      if (wasdr and !detr(x)) {
        --dr;
      }
      bool wasbc = badc(y);
      bool wasdc = detc(y);
      --cntc[y][(x & 1) ^ cur];
      if (wasbc and !badc(y)) {
        --bc;
      }
      if (wasdc and !detc(y)) {
        --dc;
      }
      --tot[(x + y + cur) & 1];
    }
    if (t == 0 or t == 1) {
      a[make_pair(x, y)] = t;
      bool wasbr = badr(x);
      bool wasdr = detr(x);
      ++cntr[x][(y & 1) ^ t];
      if (!wasbr and badr(x)) {
        ++br;
      }
      if (!wasdr and detr(x)) {
        ++dr;
      }
      bool wasbc = badc(y);
      bool wasdc = detc(y);
      ++cntc[y][(x & 1) ^ t];
      if (!wasbc and badc(y)) {
        ++bc;
      }
      if (!wasdc and detc(y)) {
        ++dc;
      }
      ++tot[(x + y + t) & 1];
    }
    if (br == 0) inc(ans, pwr(2, n - dr));
    if (bc == 0) inc(ans, pwr(2, m - dc));
    if (tot[0] == 0) dec(ans, 1);
    if (tot[1] == 0) dec(ans, 1);
    cout << ans << '\n';
  }
  return 0;
}

### Prompt

Your task is to create a CPP solution to the following problem:

A matrix of size n × m, such that each cell of it contains either 0 or 1, is considered beautiful if the sum in every contiguous submatrix of size 2 × 2 is exactly 2, i. e. every "square" of size 2 × 2 contains exactly two 1's and exactly two 0's.

You are given a matrix of size n × m. Initially each cell of this matrix is empty. Let's denote the cell on the intersection of the x-th row and the y-th column as (x, y). You have to process the queries of three types:

  * x y -1 — clear the cell (x, y), if there was a number in it; 
  * x y 0 — write the number 0 in the cell (x, y), overwriting the number that was there previously (if any); 
  * x y 1 — write the number 1 in the cell (x, y), overwriting the number that was there previously (if any). 



After each query, print the number of ways to fill the empty cells of the matrix so that the resulting matrix is beautiful. Since the answers can be large, print them modulo 998244353.

Input

The first line contains three integers n, m and k (2 ≤ n, m ≤ 10^6; 1 ≤ k ≤ 3 ⋅ 10^5) — the number of rows in the matrix, the number of columns, and the number of queries, respectively.

Then k lines follow, the i-th of them contains three integers x_i, y_i, t_i (1 ≤ x_i ≤ n; 1 ≤ y_i ≤ m; -1 ≤ t_i ≤ 1) — the parameters for the i-th query.

Output

For each query, print one integer — the number of ways to fill the empty cells of the matrix after the respective query, taken modulo 998244353.

Example

Input


2 2 7
1 1 1
1 2 1
2 1 1
1 1 0
1 2 -1
2 1 -1
1 1 -1


Output


3
1
0
1
2
3
6

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int add() { return 0; }
template <typename... M>
int add(int a, M... b) {
  int x = add(b...);
  return a + x - mod * (a + x >= mod);
}
int mul() { return 1; }
template <typename... M>
int mul(int a, M... b) {
  return int(1ll * a * mul(b...) % mod);
}
int pwr(int a, long long x) {
  return (x ? mul(pwr(mul(a, a), x >> 1), (x & 1 ? a : 1)) : 1);
}
int dvd(int a, int b) { return mul(a, pwr(b, mod - 2)); }
int sub(int a, int b) { return a - b + mod * (a < b); }
void inc(int& a, int b) { a = add(a, b); }
void dec(int& a, int b) { a = sub(a, b); }
vector<int> fact = {1}, invf = {1};
int C(int n, int k) {
  if (n < k or 0 > k) return 0;
  while (fact.size() <= n) {
    fact.push_back(mul(fact.back(), fact.size()));
    invf.push_back(dvd(1, fact.back()));
  }
  return mul(fact[n], invf[k], invf[n - k]);
}
const int N = 1e6 + 5;
int cntr[N][2];
int cntc[N][2];
int tot[2];
map<pair<int, int>, int> a;
bool badr(int i) { return cntr[i][0] and cntr[i][1]; }
bool detr(int i) { return cntr[i][0] or cntr[i][1]; }
bool badc(int i) { return cntc[i][0] and cntc[i][1]; }
bool detc(int i) { return cntc[i][0] or cntc[i][1]; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  int dr = 0, dc = 0, br = 0, bc = 0;
  while (q--) {
    int x, y, t;
    cin >> x >> y >> t;
    int ans = 0;
    if (a.count(make_pair(x, y))) {
      int cur = a[make_pair(x, y)];
      a.erase(make_pair(x, y));
      bool wasbr = badr(x);
      bool wasdr = detr(x);
      --cntr[x][(y & 1) ^ cur];
      if (wasbr and !badr(x)) {
        --br;
      }
      if (wasdr and !detr(x)) {
        --dr;
      }
      bool wasbc = badc(y);
      bool wasdc = detc(y);
      --cntc[y][(x & 1) ^ cur];
      if (wasbc and !badc(y)) {
        --bc;
      }
      if (wasdc and !detc(y)) {
        --dc;
      }
      --tot[(x + y + cur) & 1];
    }
    if (t == 0 or t == 1) {
      a[make_pair(x, y)] = t;
      bool wasbr = badr(x);
      bool wasdr = detr(x);
      ++cntr[x][(y & 1) ^ t];
      if (!wasbr and badr(x)) {
        ++br;
      }
      if (!wasdr and detr(x)) {
        ++dr;
      }
      bool wasbc = badc(y);
      bool wasdc = detc(y);
      ++cntc[y][(x & 1) ^ t];
      if (!wasbc and badc(y)) {
        ++bc;
      }
      if (!wasdc and detc(y)) {
        ++dc;
      }
      ++tot[(x + y + t) & 1];
    }
    if (br == 0) inc(ans, pwr(2, n - dr));
    if (bc == 0) inc(ans, pwr(2, m - dc));
    if (tot[0] == 0) dec(ans, 1);
    if (tot[1] == 0) dec(ans, 1);
    cout << ans << '\n';
  }
  return 0;
}

```