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 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
constexpr long long int MOD = 998244353LL;
long long int gcd(long long int a, long long int b) {
	return (b == 0) ? a : gcd(b, a % b);
}
long long int power(long long int base, int exp) {
	switch (exp) {
	case 0: return 1LL;
	case 1: return base % MOD;
	default: return power(base * base % MOD, exp / 2) * power(base, exp % 2) % MOD;
	}
}
int count_loop(int k, int n) {
	int current = k;
	int count = 0;
	do {
		if (current % 2 == 0) current = current / 2 + (1 << (n - 1));
		else current /= 2;
		++count;
	} while (current != k);
	return count;
}
bool can_loop_with(const std::string& str, const int loop_length) {
	std::string reversed; reversed.reserve(loop_length / 2);
	for (int i = loop_length / 2 - 1; i >= 0; --i) {
		if (str[i] == '1') reversed.push_back('0');
		else reversed.push_back('1');
	}
	for (auto i = 0; i < str.size() / (loop_length / 2); ++i) {
		const auto& word = (i % 2 == 0) ? str : reversed;
		for (auto j = 0; j < loop_length / 2; ++j) {
			if (str[i * loop_length / 2 + j] == '1' && word[j] == '0') return true;
			if (str[i * loop_length / 2 + j] == '0' && word[j] == '1') return false;
		}
	}
	return true;
}
int main() {
	int n; std::cin >> n;
	std::string x; std::cin >> x;
	std::vector<long long int> count(n, 0);
	long long int result = 0;
	long long int shortcut_count = 0;
	for (auto loop_length = 2; loop_length < n; loop_length += 2) {
		if (n % loop_length * 2 == loop_length) {
			long long int current = 0;
			for (auto i = 0; i < loop_length / 2; ++i) {
				current *= 2;
				if (x[i] == '1') current++;
				current %= MOD;
			}
			if (can_loop_with(x, loop_length)) {
				current++;
				current %= MOD;
			}
			for (auto l = 2; l < loop_length; l += 2) if (loop_length % l == 0) {
				current -= count[l];
			}
			current = (current % MOD + MOD) % MOD;
			count[loop_length] = current;
			shortcut_count += current;
			shortcut_count %= MOD;
			result += current * loop_length % MOD;
			result %= MOD;
		}
	}
	long long int as_digits = 0;
	for (const auto c : x) {
		as_digits *= 2;
		if (c == '1') as_digits++;
		as_digits %= MOD;
	}
	result += 2 * n * (as_digits - shortcut_count + 1) % MOD;
	result = (result % MOD + MOD) % MOD;

	std::cout << result << std::endl;
}