Traveler's Escape
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

Traveler has just stolen Furina's favorite dessert, and now Furina is frantically searching for the damn traveler in the whole Fontaine!


Fontaine is a country composed of grids of n rows and m columns with rows numbered 1,2,3, ... ,n and columns numbered 1,2,3, ... ,m. Our poor traveler is now in the grid with coordinate (1,1) at time 1, and expacts to reach the grid with coordinate (n,m) so that he could escape from Fontaine. During each moment, traveler can only move downwards or rightwords for any number of grids.

In order to catch traveler, Furina calls q inspectors who are scheduled to reach (x_i,y_i) at t_i to catch traveler, as long as traveler arrives, passes through, or leaves (x_i,y_i) when or after t_i, he will be caught! In addtion, Furina will close the gate of Fontaine after time k, if traveler haven't escaped before that, he will never be able to escape!

However, traveler is a math lover, so in any cases of crisis, as long as not being caught, he'd like to figure out his fastest scheme to escape, and how many schemes are there in total.

Annotation:Different travel schemes are distinguished based on the direction and distance of escaping, which is not required to be identical every moments. For instance, even if the route is the same, a difference in the execution can still result in distinct schemes.

输入描述:

This question has multiple sets of test data.

The first line is an integer T (1\leq T\leq5), representing the number of test cases.

For each test case, the first row of each set of data is n (2\leq n\leq500)m (2\leq m\leq500,q (1\leq q\leq100)k (1\leq k\leq100), as described in the above question. In the following q line, there are three numbers x_i, y_i, t_i (1\leq x_i\leq n, 1\leq y_i \leq m1\leq t_i\leq k in each line, representing the coordinates and arrival time of each inspectors.

输出描述:

For each set of data, output one line with two numbers. The first represents the number of schemes to escape, while the second represents the fastest speed's time to escape. Due to the conscience of the questioner, you only need to output the number of solutions afrer taking the modulus of 998244353. If unable to escape anyway, output only -1.
示例1

输入

复制
2
2 3 2 5
1 3 1
2 2 3
2 3 2 5
1 3 1
2 2 2

输出

复制
1 2
-1

说明

As for the first test case, the situation are mapped below, where T represents traveler and F represents the finish line:

As for the first step, traveler can move downside to the end as mapped below as mapped below:

Then, if traveler don't move rightside to the end, the other inspector will arrived when traveler are trying to escape to the finish point, so there is only one possible schemea for him, and the fastest time is 2.

As for the second test case, the situation are mapped below:
As for the first step, traveler can move downside to the end as mapped below, and then there comes another inspector as mapped below:

Then, it can be proven that he can't reach the finish point anyway, so output -1.
示例2

输入

复制
1
8 8 11 8
7 4 2
3 1 2
1 8 1
2 1 1
5 4 1
5 8 3
8 5 4
7 1 1
3 6 1
7 2 1
1 7 2

输出

复制
7763 3

备注:

it is also guaranteed that grids (1,1) and (n,m) has no inspectors, but two or even more inspectors might appear in the same grid.