Under construction

Still being built. Things may change

Chess Rush

Time limit2.3sMemory limit64 MB

Summary
Find the minimum number of moves and the count of shortest paths for a piece going from a column in row 1 to a target column in row R.
Level

Hard8 of 10

Topics
Matrix, Math, Combinatorics
Solved
No attempts yet

Statement

The mythic world of Chess Land is a rectangular grid of squares with RR rows and CC columns, where R≥CR \ge C. Rows are numbered 1 to RR, and columns are numbered 1 to CC.

The inhabitants of Chess Land are called pieces. Five types roam the land: pawns, rooks, bishops, queens and kings. Chivalry has been dead in Chess Land for a long time, so there are no knights.

Each piece moves in its own way. In one step:

  • a pawn can move one row forward (from row rr to row r+1r + 1) without changing columns;
  • a rook can move any number of columns left or right without changing rows, or any number of rows forward or backward without changing columns;
  • a bishop can move to any square on the two diagonals that cross its current square;
  • a queen can move to any square that a rook or a bishop could reach from her position;
  • a king can move to any of the 8 adjacent squares.

The figure marks with X the squares each piece can reach in one step. Rows are numbered bottom to top, and columns are numbered left to right.

Recently, pieces passing through Chess Land have been captured by unknown forces without warning and disappeared. Pieces want to reach their destinations in as few moves as possible. They also care how many different paths use that minimal number of steps, since more paths mean a lower chance of being captured. Two paths are different if they differ in at least one visited square.

A piece enters Chess Land at a given column of row 1 and exits at a given column of row RR. Answer QQ questions. Each question gives the piece type, the column c1c_1 where it enters row 1, and the column cRc_R it must reach in row RR. For each question, find the minimal number of moves and the number of different paths that use that many moves.

Input

The first line contains three integers RR, CC, and QQ, separated by spaces. Then QQ lines follow. Each line contains a character TT for the piece type (P for pawn, R for rook, B for bishop, Q for queen, K for king), followed by two integers c1c_1 and cRc_R, separated by spaces.

Output

Print QQ lines. The ii-th line contains two integers separated by a space: the minimal number of moves for the ii-th question, and the number of different paths that reach the target in that many moves, taken modulo 109+710^9 + 7. If the target cannot be reached, print 0 0.

Constraints

  • 1≤Q≤10001 \le Q \le 1000
  • 2≤C≤10002 \le C \le 1000
  • C≤R≤109C \le R \le 10^9
  • 1≤c1,cR≤C1 \le c_1, c_R \le C

Examples1

  1. Example 1

    Input
    8 8 5
    P 1 2
    R 4 8
    Q 2 3
    B 3 6
    K 5 5
    
    Expected output
    0 0
    2 2
    2 5
    2 2
    7 393