# 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 CS = AS.map do |a| BS.map do |b| case OP when :+ (a + b) % K when :* (a * b) % K end end.sum % K end puts(CS.sum % K)