#include <bits/stdc++.h>
using namespace std;
long long PosMod(long long a) { return a >= 0 ? a : a + 998244353; }
long long AddMod(long long a, long long b) {
  return PosMod((a + b) % 998244353);
}
long long MultMod(long long a, long long b) {
  return PosMod((a * b) % 998244353);
}
long long powll(long long a, long long n) {
  long long r = 1;
  for (long long b = n; b > 0; b >>= 1, a = MultMod(a, a))
    if (b & 1) r = MultMod(r, a);
  return r;
}
long long InvMod(long long a) { return powll(a, 998244353 - 2); }
long long Frac(long long a, long long b) { return MultMod(a, InvMod(b)); }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int n, m, k;
int COL[2][2][1000100];
int ROW[2][2][1000100];
map<std::pair<int, int>, int> writtenValue;
long long col_posCnt[3];
long long row_posCnt[3];
long long valid1Cnt[2];
long long valid2Cnt[2];
int calcRPos(int i) {
  int pos = 0;
  pos += ROW[1][1][i] == 0 && ROW[0][0][i] == 0;
  pos += ROW[0][1][i] == 0 && ROW[1][0][i] == 0;
  return pos;
}
int calcCPos(int i) {
  int pos = 0;
  pos += COL[1][1][i] == 0 && COL[0][0][i] == 0;
  pos += COL[0][1][i] == 0 && COL[1][0][i] == 0;
  return pos;
}
std::pair<int, int> calcValid(int i) {
  bool valid1 = ROW[1][1][i] == 0 && ROW[0][0][i] == 0;
  bool valid2 = ROW[0][1][i] == 0 && ROW[1][0][i] == 0;
  if (i % 2 == 0) swap(valid1, valid2);
  return {(int)valid1, (int)valid2};
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n >> m >> k;
  col_posCnt[2] = m;
  row_posCnt[2] = n;
  valid1Cnt[1] = n;
  valid2Cnt[1] = n;
  for (int i = 0; i < k; i++) {
    int r, c, t;
    cin >> r >> c >> t;
    int prevRPos = calcRPos(r);
    int prevCPos = calcCPos(c);
    std::pair<int, int> prevValid = calcValid(r);
    row_posCnt[prevRPos]--;
    col_posCnt[prevCPos]--;
    valid1Cnt[prevValid.first]--;
    valid2Cnt[prevValid.second]--;
    std::pair<int, int> p = {r, c};
    if (writtenValue.count(p) > 0) {
      int val = writtenValue[p];
      COL[r % 2][val][c]--;
      ROW[c % 2][val][r]--;
      writtenValue.erase(p);
    }
    if (t == 0 || t == 1) {
      writtenValue[p] = t;
      COL[r % 2][t][c]++;
      ROW[c % 2][t][r]++;
    }
    int curRPos = calcRPos(r);
    int curCPos = calcCPos(c);
    std::pair<int, int> curValid = calcValid(r);
    row_posCnt[curRPos]++;
    col_posCnt[curCPos]++;
    valid1Cnt[curValid.first]++;
    valid2Cnt[curValid.second]++;
    long long ansR = row_posCnt[0] == 0 ? powll(2, row_posCnt[2]) : 0;
    long long ansC = col_posCnt[0] == 0 ? powll(2, col_posCnt[2]) : 0;
    long long dup =
        (long long)(valid1Cnt[0] == 0) + (long long)(valid2Cnt[0] == 0);
    cout << AddMod(AddMod(ansR, ansC), -dup) << '\n';
  }
}

### Prompt

Please formulate 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;
long long PosMod(long long a) { return a >= 0 ? a : a + 998244353; }
long long AddMod(long long a, long long b) {
  return PosMod((a + b) % 998244353);
}
long long MultMod(long long a, long long b) {
  return PosMod((a * b) % 998244353);
}
long long powll(long long a, long long n) {
  long long r = 1;
  for (long long b = n; b > 0; b >>= 1, a = MultMod(a, a))
    if (b & 1) r = MultMod(r, a);
  return r;
}
long long InvMod(long long a) { return powll(a, 998244353 - 2); }
long long Frac(long long a, long long b) { return MultMod(a, InvMod(b)); }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int n, m, k;
int COL[2][2][1000100];
int ROW[2][2][1000100];
map<std::pair<int, int>, int> writtenValue;
long long col_posCnt[3];
long long row_posCnt[3];
long long valid1Cnt[2];
long long valid2Cnt[2];
int calcRPos(int i) {
  int pos = 0;
  pos += ROW[1][1][i] == 0 && ROW[0][0][i] == 0;
  pos += ROW[0][1][i] == 0 && ROW[1][0][i] == 0;
  return pos;
}
int calcCPos(int i) {
  int pos = 0;
  pos += COL[1][1][i] == 0 && COL[0][0][i] == 0;
  pos += COL[0][1][i] == 0 && COL[1][0][i] == 0;
  return pos;
}
std::pair<int, int> calcValid(int i) {
  bool valid1 = ROW[1][1][i] == 0 && ROW[0][0][i] == 0;
  bool valid2 = ROW[0][1][i] == 0 && ROW[1][0][i] == 0;
  if (i % 2 == 0) swap(valid1, valid2);
  return {(int)valid1, (int)valid2};
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n >> m >> k;
  col_posCnt[2] = m;
  row_posCnt[2] = n;
  valid1Cnt[1] = n;
  valid2Cnt[1] = n;
  for (int i = 0; i < k; i++) {
    int r, c, t;
    cin >> r >> c >> t;
    int prevRPos = calcRPos(r);
    int prevCPos = calcCPos(c);
    std::pair<int, int> prevValid = calcValid(r);
    row_posCnt[prevRPos]--;
    col_posCnt[prevCPos]--;
    valid1Cnt[prevValid.first]--;
    valid2Cnt[prevValid.second]--;
    std::pair<int, int> p = {r, c};
    if (writtenValue.count(p) > 0) {
      int val = writtenValue[p];
      COL[r % 2][val][c]--;
      ROW[c % 2][val][r]--;
      writtenValue.erase(p);
    }
    if (t == 0 || t == 1) {
      writtenValue[p] = t;
      COL[r % 2][t][c]++;
      ROW[c % 2][t][r]++;
    }
    int curRPos = calcRPos(r);
    int curCPos = calcCPos(c);
    std::pair<int, int> curValid = calcValid(r);
    row_posCnt[curRPos]++;
    col_posCnt[curCPos]++;
    valid1Cnt[curValid.first]++;
    valid2Cnt[curValid.second]++;
    long long ansR = row_posCnt[0] == 0 ? powll(2, row_posCnt[2]) : 0;
    long long ansC = col_posCnt[0] == 0 ? powll(2, col_posCnt[2]) : 0;
    long long dup =
        (long long)(valid1Cnt[0] == 0) + (long long)(valid2Cnt[0] == 0);
    cout << AddMod(AddMod(ansR, ansC), -dup) << '\n';
  }
}

```