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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <algorithm>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <ctime>
using namespace std;

#ifdef __APPLE__
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif

const int N = (int)1e6 + 123;
const int MOD = (int)1e9 + 7;
const int inf = (int)5e8;
const long long infll = (long long)1e17;

int base = 255;

int add(int a, int b) {
    long long r = a * 1ll + b;
    return (r % MOD + MOD) % MOD;
}

int mul(int a, int b) {
    return (a * 1ll * b) % MOD;
}

int get_hash(const string& s) {
    int res = 0;
    for (auto c : s) {
        res = add(mul(res, base), c);
    }
    return res;
}

void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;

    map<pair<int, int>, int> cnt;
    for (int mask = 0; mask < (1 << n); ++mask) {
        int h[2] = {0, 0};
        for (int i = 0; i < n; ++i) {
            int t = (mask & (1 << i)) > 0;
            h[t] = add(mul(h[t], base), s[n + i]);
        }
        ++cnt[{h[0], h[1]}];
    }

    long long ans = 0;
    for (int mask = 0; mask < (1 << n); ++mask) {
        int h[2] = {0, 0};
        for (int i = n - 1; i >= 0; --i) {
            int t = (mask & (1 << i)) > 0;
            h[t] = add(mul(h[t], base), s[i]);
        }
        ans += cnt[{h[1], h[0]}];
    }

    printf("%lld\n", ans);
}

int main() {
#ifdef __APPLE__
#endif
    srand(time(0));
    base += rand() % 255;
    int t = 1;
    while (t--) {
        solve();
    }
    return 0;
}