# frozen_string_literal: true in_n = gets.chomp.to_i in_lr = in_n.times.map { gets.chomp.split.map(&:to_i) } puts ( (0..(in_n - 1)) .to_a .permutation(in_n) .count do |order| diff = 0 possible = true order.each do |i| l, r = in_lr[i] if diff > r possible = false break elsif diff < l diff = l end end possible end )