Shino and K-Special Cells

2.7

14 votes
Combinatorics, Easy, Math
Problem

Today Jin has given a task to Shino, Shino has to travel from cell (1,1) to cell (N,M) in a grid of size NM. But in order make this task interesting for Shino, Jin has decided to keep some special candies in some K special cells of the grid, where each candy has an amount of happiness associated with it.

Shino can travel only in right & down direction in the grid, as he is too careful & does not want to fall out of grid. Now, we call the value of a path the happiness of all cells lying on the path. All non-special cells have happiness equal to 0.

Now, you need to find and print the sum of the values of all paths from (1,1) to (N,M), traveling only right and down to an adjacent cell.

As Shino is not good at counting help him find the answer.

Input Format

The first Line contains a single integer T denoting number of test cases

The next line contains 3 space separated integers, N, M and K where N * M is the size of grid & K denoting number of special cells

The next K lines contains three integers Xi,Yi,Hi where (Xi,Yi) is cell coordinate & Hi is the amount of happiness Shino will get from a candy at cell (Xi,Yi)

Constraints

1T3

1N,M,K105

1XiN,1YiM

1Hi105

Output Format

For each test case you will output a single integer denoting the total amount of happiness Shino will get. As the answer can be quiet large you can output answer modulo 109+7

Sample Input
1
2 2 2
1 2 4
2 1 7
Sample Output
11
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

.

Editor Image

?