use std::collections::*; #[allow(unused)] struct Scanner { stack: VecDeque, input: std::io::Stdin, } impl Scanner { fn new() -> Scanner { Scanner { stack: VecDeque::::new(), input: std::io::stdin(), } } fn take(&mut self) -> Option { if let Some(s) = self.stack.pop_front() { return Some(s); } self.enqueue_from_line(); self.stack.pop_front() } fn enqueue_from_line(&mut self) { let mut t = String::new(); self.input.read_line(&mut t).ok(); t.trim() .split_ascii_whitespace() .for_each(|s| self.stack.push_back(s.to_owned())); } } macro_rules! scan { ($io:expr => $t:ty) => { $io.take().unwrap().parse::<$t>().unwrap() }; ($io:expr => $t:tt * $n:expr) => ((0..$n).map(|_| scan!($io => $t)).collect::>()); ($io:expr => $($t:tt),*) => (($(scan!($io => $t)),*)); } fn main() { let mut cin = Scanner::new(); let n = scan!(cin => i32); let aa = scan!(cin => i32 * n); let mut one = 0 as i64; let mut two = 0 as i64; let mut three = 0 as i64; for a in aa { match a { 1 => one += 1, 2 => two += 1, _ => three += 1, } } let mut total = 0; total += 2 * one * (one - 1) / 2; total += 1 * two * (two - 1) / 2; total += 1 * three * (three - 1) / 2; total += 3 * one * two; total += 2 * one * three; total += 1 * two * three; println!("{}", total); }