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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto x : v) cerr << x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int i = 0; i < (n); ++i) cerr << a[i] << ", "; cerr << endl
typedef long long ll;
typedef long double ld;
char S[45];
const int MAXN = 45;
const ll P1 = 998244353;
const ll Q1 = 107;
const ll P2 = 1000000007;
const ll Q2 = 103;
ll p1[MAXN];
ll p2[MAXN];
int main() {
	p1[0] = 1;
	p2[0] = 1;
	for (int i = 1; i < MAXN; ++i) {
		p1[i] = p1[i - 1] * Q1 % P1;
		p2[i] = p2[i - 1] * Q2 % P2;
	}
	map<pair<int, pair<ll, ll>>, int> H;
	int N;
	scanf("%d%s", &N, S);
	for (int i = 0; i < (1 << N); ++i) {
		ll x1 = 0, y1 = 0;
		ll x2 = 0, y2 = 0;
		for (int j = 0; j < N; ++j) {
			if (i & (1 << j)) {
				x1 = x1 * Q1 + (S[j] - 'a');
				x1 %= P1;
				x2 = x2 * Q2 + (S[j] - 'a');
				x2 %= P2;
			}
		}
		for (int j = N - 1; j >= 0; --j) {
			if (!(i & (1 << j))) {
				y1 = y1 * Q1 + (S[j] - 'a');
				y1 %= P1;
				y2 = y2 * Q2 + (S[j] - 'a');
				y2 %= P2;
			}
		}
		int xlen = __builtin_popcount(i);
		ll h = (x1 * p1[N - xlen] - y1) % P1;
		if (h < 0) h += P1;
		ll h2 = (x2 * p2[N - xlen] - y2) % P2;
		if (h2 < 0) h2 += P1;
		++H[{xlen, {h, h2}}];
	}
	ll ans = 0;
	for (int i = 0; i < (1 << N); ++i) {
		ll x1 = 0, y1 = 0;
		ll x2 = 0, y2 = 0;
		for (int j = 0; j < N; ++j) {
			if (i & (1 << j)) {
				x1 = x1 * Q1 + (S[N + j] - 'a');
				x1 %= P1;
				x2 = x2 * Q2 + (S[N + j] - 'a');
				x2 %= P2;
			}
		}
		for (int j = N - 1; j >= 0; --j) {
			if (!(i & (1 << j))) {
				y1 = y1 * Q1 + (S[N + j] - 'a');
				y1 %= P1;
				y2 = y2 * Q2 + (S[N + j] - 'a');
				y2 %= P2;
			}
		}
		int xlen = __builtin_popcount(i);
		int rem = N - xlen;
		ll h = (y1 * p1[xlen] - x1) % P1;
		if (h < 0) h += P1;
		ll h2 = (y2 * p2[xlen] - x2) % P2;
		if (h2 < 0) h2 += P2;
		auto it = H.find({rem, {h, h2}});
		if (it != H.end()) ans += it->second;
	}
	printf("%lld\n", ans);
}