Problem:
Score : 800 points

Problem Statement

Given are integers N and X. For each integer k between 0 and X (inclusive), find the answer to the following question, then compute the sum of all those answers, modulo 998244353.

Let us repeat the following operation on the integer k. Operation: if the integer is currently odd, subtract 1 from it and divide it by 2; otherwise, divide it by 2 and add 2^{N-1} to it. How many operations need to be performed until k returns to its original value? (The answer is considered to be 0 if k never returns to its original value.)

Constraints

1 \leq N \leq 2\times 10^5

0 \leq X < 2^N

X is given in binary and has exactly N digits. (In case X has less than N digits, it is given with leading zeroes.)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X

Output

Print the sum of the answers to the questions for the integers between 0 and X (inclusive), modulo 998244353.

Sample Input 1

3
111

Sample Output 1

40

For example, for k=3, the operation changes k as follows: 1,0,4,6,7,3. Therefore the answer for k=3 is 6.

Sample Input 2

6
110101

Sample Output 2

616

Sample Input 3

30
001110011011011101010111011100

Sample Output 3

549320998

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include<bitset>
#include <string>

using namespace std;

int period(int K, int N) {

	long long int x = K;
	int cnt = 0;

	for (;;) {
		if (x % 2 == 1) { x = (x - 1) / 2; }
		else { x = x / 2 + ((long long int) 1 << (N - 1)); }
		cnt++;
		if (x == K) { break; }
	}
	return cnt;
}

//const int N =12;

int N;
string X;

long long int C[400005];

long long int MOD = 998244353;

long long int NUM(int M) {
	long long int tmp = 0;

	for (int i = 0; i < M; i++) {
		tmp *= 2; tmp %= MOD;
		tmp += X[i] - '0';
	}

	string str;
	str = X.substr(0, M);

	string c_str;
	for (int i = 0; i < (int)str.size(); i++) {
		if (str[i] == '1') { c_str += '0'; }
		else { c_str += '1'; }
	}

	string S;
	int p = 0;
	while (S.size() < X.size()) {
		if (p == 0) { S += str; }
		else { S += c_str; }
		p = (p + 1) % 2;
	}

	//cout << "S= " << S << endl;

	if (S <= X) { tmp++; }

	return tmp%MOD;
}

long long int cnt_X(void) {
	long long int ret = 0;
	for (int i = 0; i <(int) X.size(); i++) {
		ret *= 2; ret %= MOD;
		ret += X[i] - '0';
	}
	return (ret+1)%MOD;
}

int main(void) {

	/*for (int x = 0; x < (1<<N); x++) {
		if (period(x, N) == 2 * N) { continue; }
		cout << x <<" "<< bitset<N>(x)<< " " << period(x, N) << endl;
	}*/

	cin >> N >> X;

	for (int i = 0; i <= N * 2; i++) { C[i] = -1; }

	for (int P = 2; (P/2)*3 <= N; P += 2) {

		if ((N - (P / 2) * 3) % P != 0) { continue; }

		//cout << "P= " << P << endl;

		int M = P / 2;

		C[P] = NUM(M);

	}

	C[N * 2] = cnt_X();

	/*for (int i = 1; i <= N*2; i++) {
		cout << "C[" << i << "]= " << C[i] << endl;
	}*/

	for (int i = 1; i <= N * 2; i++) {
		if (C[i] >0) {
			for (int j = 2; j*i <= N * 2; j++) {
				if (C[j*i] == -1) { continue; }
				C[j*i] = (C[j*i] - C[i] + MOD) % MOD;
			}
		}
	}

	/*for (int i = 1; i <= N * 2; i++) {
		cout << "C[" << i << "]= " << C[i] << endl;
	}*/

	long long int ans = 0;

	for (int i = 1; i <= N * 2; i++) {
		if (C[i] > 0) { ans += C[i] * i; ans %= MOD; }
	}

	cout << ans << endl;

	return 0;
}