eolymp
bolt
Try our new interface for solving problems
Problems

K-Special Cells

K-Special Cells

You are given a matrix which has $k$ special cells in it. You have to reach $(n, m)$ from $(1, 1)$. From any cell, you can only move rightwards or downwards. The $k$ special cells are those cells in this grid which have special strength at them. $i$-th special cell has $p_i$ units of strength and if you travel through this cell, you store the strength. Find the total strength you can store after travelling through all the possible paths in the grid to reach cell $(n, m)$. Note that: \begin{itemize} \item The strength of a path is the sum of strength $p_i$ of all the special cells that are visited in this path. \item The cells that are not special have power quotient equals to zero. \end{itemize} \InputFile The first line contains the total number of test cases $t$. The first line of each test case contains three space separated integers $n, m\:(1 \le n, m \le 10^5)$ and $k\:(1 \le k \le 10^6)$ where $n * m$ is the size of grid and $k$ is the total number of special cells in the grid. Each of next $k$ lines contains $x_i, y_i\:(1 \le x_i \le n, 1 \le y_i \le m)$, and $p_i\:(1 \le p_i \le 10^5)$ where $(x_i, y_i)$ is the location of special cell and $p_i$ is the cell strength. \OutputFile For each test case, print in a new line a single integer representing the total strength that you can store, as the total strength can be too large, print it modulo $10^9 + 7$.
Time limit 1 second
Memory limit 128 MiB
Input example #1
1
2 2 2
1 2 4
2 1 7
Output example #1
11