N = gets.to_i A = Array.new(N) { gets.split.map(&:to_i) } def is_group(n, op) # 結合律 r = 0 ... n return false unless r.all? { |a| r.all? { |b| r.all? { |c| op[a][op[b][c]] == op[op[a][b]][c] } } } identity = r.find { |e| r.all? { |a| op[a][e] == a and op[e][a] == a } } or return false return false unless r.all? { |a| r.any? { |i| op[a][i] == identity and op[i][a] == identity } } true end puts is_group(N, A) ? "Yes" : "No"