Problem:
Score : 600 points

Problem Statement

You are given a string S of length 2N consisting of lowercase English letters.

There are 2^{2N} ways to color each character in S red or blue. Among these ways, how many satisfy the following condition?

The string obtained by reading the characters painted red from left to right is equal to the string obtained by reading the characters painted blue from right to left.

Constraints

1 \leq N \leq 18

The length of S is 2N.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to paint the string that satisfy the condition.

Sample Input 1

4
cabaacba

Sample Output 1

4

There are four ways to paint the string, as follows:

cabaacba

cabaacba

cabaacba

cabaacba

Sample Input 2

11
mippiisssisssiipsspiim

Sample Output 2

504

Sample Input 3

4
abcdefgh

Sample Output 3

0

Sample Input 4

18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Sample Output 4

9075135300

The answer may not be representable as a 32-bit integer.

Solution in Rust (1.15.1) (judge verdict: Accepted)

use io::*;
use std::collections::BTreeMap;

fn main() {
    let con = read_string();
    let mut sc = Scanner::new(&con);

    let n: usize = sc.next();
    let s: Vec<char> = sc.next::<String>().chars().collect();

    let mut ll: BTreeMap<(Vec<char>, Vec<char>), i64> = BTreeMap::new();
    let mut rr: BTreeMap<(Vec<char>, Vec<char>), i64> = BTreeMap::new();

    for i in 0..(1 << n) {
        let mut r = vec![];
        let mut b = vec![];
        for j in 0..n {
            if i & (1 << j) != 0 {
                r.push(s[j]);
            } else {
                b.push(s[j]);
            }
        }

        *(ll.entry((r, b)).or_insert(0)) += 1;
    }

    // println!("{:?}", ll);

    for i in 0..(1 << n) {
        let mut r = vec![];
        let mut b = vec![];
        for j in 0..n {
            if i & (1 << j) != 0 {
                r.push(s[n + j]);
            } else {
                b.push(s[n + j]);
            }
        }
        r.reverse();
        b.reverse();

        *(rr.entry((r, b)).or_insert(0)) += 1;
    }

    let mut ans = 0;
    for e in ll.iter() {
        let t = e.0.clone();
        if let Some(r) = rr.get(&(t.1, t.0)) {
            ans += e.1 * r;
        }
    }

    println!("{}", ans);
}

//-----

pub mod io {
    use std;
    use std::str::FromStr;

    pub struct Scanner<'a> {
        iter: std::str::SplitWhitespace<'a>,
    }

    impl<'a> Scanner<'a> {
        pub fn new(s: &'a str) -> Scanner<'a> {
            Scanner {
                iter: s.split_whitespace(),
            }
        }

        pub fn next<T: FromStr>(&mut self) -> T {
            let s = self.iter.next().unwrap();
            if let Ok(v) = s.parse::<T>() {
                v
            } else {
                panic!("Parse error")
            }
        }

        pub fn next_vec_len<T: FromStr>(&mut self) -> Vec<T> {
            let n: usize = self.next();
            self.next_vec(n)
        }

        pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.next()).collect()
        }
    }

    pub fn read_string() -> String {
        use std::io::Read;

        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }

    pub fn read_line() -> String {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        s.trim_right().to_owned()
    }
}