fn main() { let (n, m, k) = get_nmk(); let (operator, row) = get_row(k); let column = get_column(n, k); let ans = get_ans(operator, row, column, n, m, k); println!("{}", ans); } fn add(row: i64, column: i64, n: i64, m: i64, k: i64) -> i64 { (row * n + column * m) % k } fn mul(row: i64, column: i64, k: i64) -> i64 { row * column % k } fn get_ans(op: Operator, row: i64, column: i64, n: i64, m: i64, k: i64) -> i64 { match op { Operator::Add => add(row, column, n, m, k), Operator::Mul => mul(row, column, k), } } fn get_column(n: i64, k: i64) -> i64 { (0..n) .map(|_| { read_line() .into_iter() .next() .unwrap() .parse::() .unwrap() }) .sum::() % k } fn get_row(k: i64) -> (Operator, i64) { let mut iter = read_line().into_iter(); let op = match iter.next().unwrap() { a if a == "+" => Operator::Add, _ => Operator::Mul, }; ( op, iter.map(|num| num.parse::().unwrap()).sum::() % k, ) } enum Operator { Add, Mul, } fn get_nmk() -> (i64, i64, i64) { let mut iter = read_line().into_iter(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) } fn read_line() -> Vec { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|e| e.to_string()) .collect::>() }