Problem:
Score : 800 points

Problem Statement

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.

To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:

The 0 key: a letter 0 will be inserted to the right of the string.

The 1 key: a letter 1 will be inserted to the right of the string.

The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.

Sig has launched the editor, and pressed these keys N times in total. As a result, the editor displays a string s. Find the number of such ways to press the keys, modulo 10^9 + 7.

Constraints

1 ≦ N ≦ 5000

1 ≦ |s| ≦ N

s consists of the letters 0 and 1.

Partial Score

400 points will be awarded for passing the test set satisfying 1 ≦ N ≦ 300.

Input

The input is given from Standard Input in the following format:

N
s

Output

Print the number of the ways to press the keys N times in total such that the editor displays the string s in the end, modulo 10^9+7.

Sample Input 1

3
0

Sample Output 1

5

We will denote the backspace key by B. The following 5 ways to press the keys will cause the editor to display the string 0 in the end: 00B, 01B, 0B0, 1B0, BB0. In the last way, nothing will happen when the backspace key is pressed.

Sample Input 2

300
1100100

Sample Output 2

519054663

Sample Input 3

5000
01000001011101000100001101101111011001000110010101110010000

Sample Output 3

500886057

Solution in C++14 (GCC 5.4.1) (judge verdict: Memory Limit Exceeded)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;

int add(int x, int y) {
	x += y;
	if (x >= mod)
		x -= mod;
	return x;
}

int sub(int x, int y) {
	x -= y;
	if (x < 0)
		x += mod;
	return x;
}

int mul(ll x, ll y) {
	return x * y % mod;
}

int bin(int x, int a) {
	int y = 1;
	while (a) {
		if (a & 1)
			y = mul(y, x);
		x = mul(x, x);
		a >>= 1;
	}
	return y;
}

int inv(int x) {
	assert(x != 0);
	return bin(x, mod - 2);
}

const int M = 5050;

int d[M][M], h[M], f[M][M];
int c[M][M], a[M][2 * M], fact[2 * M];
int n, m;
int prod[2 * M];

void read() {
	string s;
	cin >> n >> s;
	m = s.length();
}

void pre() {
	for (int i = 0; i < M; ++i)
		c[i][0] = 1;
	for (int i = 1; i < M; ++i)
		for (int j = 1; j < M; ++j)
			c[i][j] = add(c[i - 1][j], c[i - 1][j - 1]);
	
	fact[0] = 1;
	for (int i = 1; i < 2 * M; ++i)
		fact[i] = mul(i, fact[i - 1]);
	
	a[0][0] = 1;
	
	prod[0] = 1;
	for (int i = 1; i < 2 * M; ++i)
		prod[i] = mul(sub(1, 2 * (i - 1)), prod[i - 1]);
	
	
	for (int k = 0; k < 2 * M; ++k) {
		int bon = bin(sub(0, 4), k);
		bon = mul(bon, prod[k]);
		//for (int t = 0; t < k; ++t)
		//	bon = mul(bon, sub(1, 2 * t));
		bon = mul(bon, inv(fact[k]));
		bon = mul(bon, inv(bin(2, k)));
		
		a[1][k] = bon;
	}
	
	for (int i = 2; i < M; ++i)
		for (int j = 0; j < 2 * M; ++j) {
			a[i][j] = a[i - 2][j];
			if (j > 0)
				a[i][j] = sub(a[i][j], mul(4, a[i - 2][j - 1]));
		}
}

void calc() {
	int by = inv(bin(2, m));
	for (int i = 0; i < M; ++i)
		for (int k = 0; k <= m; ++k) {
			int bon = a[k][i + m];
			bon = mul(bon, k % 2 == 1 ? mod - 1 : 1);
			bon = mul(bon, c[m][k]);
			h[i] = add(h[i], bon);
		}
	
	for (int i = 0; i < M; ++i)
		h[i] = mul(h[i], by);
	
}
	

void kill() {
	d[0][0] = 1;
	for (int i = 1; i < M; ++i)
		for (int j = 0; j < M; ++j) {
			if (j > 0)
				d[i][j] = add(d[i][j], mul(2, d[i - 1][j - 1]));
			if (j + 1 < M)
				d[i][j] = add(d[i][j], d[i - 1][j + 1]);
	}
	
	f[0][0] = 1;
	for (int i = 1; i < M; ++i)
		for (int j = 0; j < M; ++j) {
			if (j > 0)
				f[i][j] = add(f[i][j], mul(2, f[i - 1][j - 1]));
			if (j + 1 < M)
				f[i][j] = add(f[i][j], f[i - 1][j + 1]);
			if (j == 0)
				f[i][j] = add(f[i][j], f[i - 1][j]);
	}
	

	int s = n - m;
		
	int ans = 0;
	for (int i = 0; 2 * i <= s; ++i) {
		int bon = mul(h[i], bin(2, i));
		ans = add(ans, mul(bon, f[s - 2 * i][0]));
	}
	cout << ans << endl;
}

int main() {
	pre();
	read();
	calc();
	kill();	
	return 0;
}