use proconio::input; fn fast_pow_mod(base: u128, exp: u128, modulo: u128) -> u128{ if modulo == 0 { return 0; } if exp == 0 { return 1; } else if exp == 1 { return base % modulo; } else if exp % 2 == 0 { return ((fast_pow_mod(base, exp/2, modulo) % modulo) * (fast_pow_mod(base, exp/2, modulo) % modulo)) % modulo; } else { return ((fast_pow_mod(base, (exp-1)/2, modulo) % modulo) * (fast_pow_mod(base, (exp-1)/2, modulo) % modulo) * fast_pow_mod(base, 1, modulo)) % modulo; } } fn main () { input! { n: usize, p: u128, q: u128, mut arr: [u128; n], } arr.sort(); let mut apow: Vec = Vec::new(); let mut bpow: Vec = Vec::new(); let mut cpow: Vec = Vec::new(); let mut dpow: Vec = Vec::new(); for i in 1..(arr[n-1]+1) { apow.push(fast_pow_mod(10, i as u128, p)); bpow.push(fast_pow_mod(9, i as u128, p)); cpow.push(fast_pow_mod(7, i as u128, p)); dpow.push(fast_pow_mod(5, i as u128, p)); } let mut ans: u64 = 0; for i in 0..n { let a = arr[i]; for j in i+1..n { let b = arr[j]; for k in j+1..n { let c = arr[k]; for l in k+1..n { let d = arr[l]; //println!("a: {}, b: {}, c: {}, d: {}",a,b,c,d); let val = (apow[a as usize -1] + bpow[b as usize -1] + cpow[c as usize -1] + dpow[d as usize -1]) % p; if val == q { ans += 1; } } } } } println!("{}", ans); }