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 <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#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
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
	return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
const int MAXN = 200005;
const ll MOD = 998244353;
char str[MAXN];
int y[MAXN];
struct Mint {
	int val;
	Mint() { val = 0; }
	Mint(const ll& x) {
    val = (-MOD <= x && x <= MOD) ? x : x % MOD;
		if (val < 0) val += MOD;
  }
	friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
	friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
	friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
	Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
  Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
  Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
  friend Mint pow(Mint a, ll p) {
		Mint ans = 1;
		for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
		return ans;
	}
  Mint& operator/=(const Mint& m) { return *this *= pow(m, MOD - 2); }
  Mint& operator++() { return *this += 1; }
  Mint& operator--() { return *this -= 1; }
  Mint operator++(int) { Mint result(*this); *this += 1; return result; }
  Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
  Mint operator-() const { return Mint(-val); }
  friend Mint operator+(Mint a, const Mint& b) { return a += b; }
	friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
	friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
	friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
	friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
};
Mint g[MAXN * 2], p2[MAXN * 2];
int main() {
	p2[0] = 1;
	for (int i = 1; i < MAXN * 2; ++i) p2[i] = p2[i - 1] * 2;
	int n;
	scanf("%d%s", &n, str);
	for (int d = 1; d < 2 * n; ++d) {
		if (2 * n % d || n % d == 0) {
			g[d] = 0;
			continue;
		}	
		assert(d % 2 == 0);
		// y[i] = y[i + d]
		for (int i = 0; i < d / 2; ++i) {
			if (str[i] == '1') {
				g[d] += p2[d / 2 - i - 1];
			}
			y[i] = str[i] - '0';
		}
		bool same = true;
		for (int i = d / 2; i < n; ++i) {
			y[i] = 1 - y[i - d / 2];
			if (y[i] < str[i] - '0') {
				break;
			}
			if (y[i] > str[i] - '0') {
				same = false;
				break;
			}
		}
		if (same) ++g[d];
		for (int e = d + d; e <= 2 * n; e += d) {
			g[e] -= g[d];
		}
	}
	++g[2 * n];
	for (int i = 0; i < n; ++i) {
		if (str[i] == '1') {
			g[2 * n] += p2[n - i - 1];
		}
	}
	Mint ans = 0;
	for (int d = 1; d <= 2 * n; ++d) {
		ans += g[d] * d;
	}
	printf("%lld\n", ans.val);
}