Under construction

Still being built. Things may change

Latin Squares

Interview

Time limit0.5sMemory limit2048 MB

Summary
Given T row and column swaps applied in order, output any Latin square of size N that would produce the final matrix, or print * if none exists.
Level

Easy3 of 10

Topics
Array, Simulation, Implementation
Solved
No attempts yet

Problem

A Latin square of size NN is an N×NN \times N matrix where each entry is an integer between 11 and NN, and no two entries in the same row or column are equal.

Alice has a Latin square of size NN and applied a list of transformations to it. Each transformation swaps either two distinct rows or two distinct columns of the matrix. The transformations are applied in order: the first one is applied to the original matrix, the second to the result of the first, and so on. The final result is an N×NN \times N matrix.

Alice does not reveal the matrix, only the list of transformations. Determine whether an original matrix exists that becomes the final matrix after all transformations are applied. If one exists, output any possible content of the original matrix.

Input

The first line contains two integers NN (2≤N≤5002 \le N \le 500) and TT (1≤T≤1051 \le T \le 10^5), the size of the Latin square and the number of transformations.

Each of the next TT lines describes one transformation, in the order they are applied. A line contains a character XX and two integers II and JJ (1≤I,J≤N1 \le I, J \le N, I≠JI \ne J). If XX is R, rows II and JJ are swapped. If XX is C, columns II and JJ are swapped.

Output

If an original matrix exists that becomes the final matrix after all transformations, output NN lines with NN integers each, representing any such matrix.

If no such matrix exists, output a single line containing the character * (asterisk).

Examples2

  1. Example 1

    Input
    4 4
    R 1 2
    C 2 1
    R 3 4
    C 3 4
    
    Expected output
    1 2 3 4
    2 1 4 3
    3 4 1 2
    4 3 2 1
    
  2. Example 2

    Input
    4 3
    R 1 2
    R 1 2
    R 2 1
    
    Expected output
    *