# frozen_string_literal: true N, M, K = gets.chomp.split.map(&:to_i) ROW = gets.chomp.split OP = ROW[0].intern BS = ROW[1..-1].map(&:to_i) AS = N.times.map do gets.to_i end puts(case OP when :+ AS.map do |a| BS.map do |b| (a + b) % K end.sum % K end.sum % K when :* AS.map do |a| if (a % K).zero? 0 else BS.map do |b| if (b % K).zero? 0 else (a * b) % K end end.sum % K end end.sum % K end)