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

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <stdlib.h>
using namespace std;
typedef long long int lld;
typedef long double ld;
const lld mod = 998244353;
lld dp[222222],vis[222222];
bool can(string a,string b){
	for(int e=0;e<a.size();e++){
		if(a[e]>b[e]) return true;
		if(a[e]<b[e]) return false;
	}
	return true;
}
int main(void){
	lld n;
	string a;
	cin >> n >> a;
	dp[0] = 1;
	for(int e=1;e<222222;e++) dp[e] = (dp[e-1]*2)%mod;
	lld ans = 0;
	for(int e=0;e<n;e++) if(a[e]=='1') ans = (ans+dp[n-e-1]*n*2)%mod;
	ans = (ans+2*n)%mod;
	vector<lld> v;
	for(int e=1;e<n;e++) if(n%e==0&&(n/e)%2) v.push_back((lld)e);
	for(int e=0;e<v.size();e++){
		lld val = 0;
		for(int p=0;p<v[e];p++) if(a[p]=='1') val = (val+dp[v[e]-p-1])%mod;
		string f = "",s = "";
		for(int p=0;p<v[e];p++) f += a[p];
		for(int p=0;p<v[e];p++){
			if(f[p]=='1') f[p] = '0';
			else f[p] = '1';
		}
		for(int p=v[e];p<v[e]*2;p++) s += a[p];
		if(can(s,f)) val = (val+1)%mod;
		val = (val-vis[v[e]]+mod)%mod;
		ans = (ans - 2*val*n + 2*val*v[e]+mod)%mod;
		for(int p=v[e];p<=n;p+=v[e]) vis[p] = (vis[p]+val)%mod;
	}
	printf("%lld",ans);
	return 0;
}