Under construction

Still being built. Things may change

Robogolf

Time limit3sMemory limit512 MB

Summary
Two robots alternately move a chip right or down across an n by m grid with priced traps, and the task is to sum the game value over every starting square.
Level

Hard9 of 10

Topics
Dynamic programming, Sorting, Math
Solved
No attempts yet

Statement

The World Robot Olympiad holds robogolf competitions. The playing field is a rectangle of n×mn \times m unit squares. Rows are numbered 1 to nn, and columns are numbered 1 to mm. Each square is identified by its row rr and column cc.

Two robots take turns moving a special chip across the field. Some squares contain traps. When the chip is on square (r,c)(r, c), the robot whose turn it is can move it to (r+1,c)(r+1, c) or to (r,c+1)(r, c+1). If that square does not exist because the chip is in the last row or the last column, the robot can move the chip off the field. The game ends when the chip is off the field or on a square with a trap.

Each trap has an integer price viv_i. The game result is the price of the trap where the game ended, or 0 if the chip left the field. The robot that moves first wants to minimize the result, and the robot that moves second wants to maximize it.

Let the chip start on square (r,c)(r, c). The guaranteed result g(r,c)g(r, c) is the smallest result the first robot can force, no matter how the opponent plays. Since the starting square is unknown in advance, the developers want the sum of g(r,c)g(r, c) over all squares. Given the positions and prices of the traps, compute this sum.

Input

The first line contains three integers nn, mm, and kk (1≤n,m≤1091 \le n, m \le 10^9; 1≤k≤min⁡(n⋅m,105)1 \le k \le \min(n \cdot m, 10^5)), the number of rows, columns, and traps. Each of the next kk lines contains three integers rir_i, cic_i, and viv_i (1≤ri≤n1 \le r_i \le n, 1≤ci≤m1 \le c_i \le m, −109≤vi≤109-10^9 \le v_i \le 10^9), the row, column, and price of the ii-th trap. No two traps share a square.

Output

Print one integer, the sum of g(r,c)g(r, c) over all squares modulo 998 244 353998\,244\,353. The remainder r=a mod br = a \bmod b for a possibly negative aa is the value in [0,b−1][0, b-1] such that a=qb+ra = qb + r for some integer qq. For example, 13 mod 4=113 \bmod 4 = 1, −13 mod 4=3-13 \bmod 4 = 3, 12 mod 4=012 \bmod 4 = 0, and −12 mod 4=0-12 \bmod 4 = 0.

Hint

In the first example, the guaranteed results for all squares are shown in the figure below, with trap squares highlighted.

The sum of the results is 1+1−2+0−2−2+3+0+0=−11 + 1 - 2 + 0 - 2 - 2 + 3 + 0 + 0 = -1. The answer is (−1) mod 998 244 353=998 244 352(-1) \bmod 998\,244\,353 = 998\,244\,352.

In the second example, the guaranteed results for all squares are shown in the figure below, with trap squares highlighted.

The sum of the results is 1+2+0−3+1+0−3−3=−51 + 2 + 0 - 3 + 1 + 0 - 3 - 3 = -5. The answer is (−5) mod 998 244 353=998 244 348(-5) \bmod 998\,244\,353 = 998\,244\,348.

Examples2

  1. Example 1

    Input
    3 3 3
    2 3 -2
    3 1 3
    1 2 1
    
    Expected output
    998244352
    
  2. Example 2

    Input
    2 4 3
    1 2 2
    2 4 -3
    2 1 1
    
    Expected output
    998244348