CSES - Aalto Competitive Programming 2024 - wk12 - Wed - Grid Sort
  • Time limit: 1.00 s
  • Memory limit: 512 MB

You are given an n×nn \times n matrix that contains the numbers 1, , n21,\ \dots,\ n^2. The number at the intersection of the ii-th row and jj-th column is ai,ja_{i,j}. Your task is to sort the matrix by performing any of the following operations arbitrarily many times:

  1. Pick two rows and swap them.
  2. Pick two columns and swap them.

The matrix is considered sorted is for each pair cells (x1,y1)(x1, y1), (x2,y2)(x2, y2) it holds that ax1,y1>ax2,y2a_{x1,y1} > a_{x2,y2} if x1>x2x1 > x2. If x1=x2x1 = x2 then ax1,y1>ax2,y2a_{x1,y1} > a_{x2,y2} if y1>y2y1 > y2. That is, sort the matrix like this:

1 2 3
4 5 6
7 8 9

Input

The first line contains a single integer nn: The size of the matrix.
The following nn lines describe the matrix: each line contains nn integers.

Output

Print "Yes" if the matrix can be sorted and "No" otherwise.

Constraints

  • 1n1001 \leq n \leq 100

Example 1

Input:

1
1

Output:

Yes

Example 2

Input:

3
6 5 4 
3 2 1 
9 8 7 

Output:

Yes

Example 2

Input:

4
6 8 13 11 
3 15 16 4 
7 5 1 12 
2 9 14 10

Output:

No