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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;

class C
{
	static void Main()
	{
		var n = int.Parse(Console.ReadLine());
		var x = Console.ReadLine();

		var r = n;
		while (r % 2 == 0) r /= 2;
		var b = 2 * n / r;
		var cycles = Enumerable.Range(1, r).Where(i => r % i == 0).Select(i => b * i).ToArray();
		var counts = new MInt[cycles.Length];

		for (int i = 0; i < cycles.Length; i++)
		{
			var t = cycles[i] / 2;
			var sub = x.Substring(0, t);

			MInt p = 1;
			for (int j = t - 1; j >= 0; j--, p *= 2)
				counts[i] += p * (sub[j] - '0');

			var subs = new[] { sub, new string(sub.Select(c => c == '0' ? '1' : '0').ToArray()) };
			var x2 = string.Join("", Enumerable.Range(0, n / t).Select(j => subs[j % 2]));
			if (x2.CompareTo(x) <= 0) counts[i] += 1;
		}
		for (int i = 0; i < cycles.Length; i++)
			for (int j = i + 1; j < cycles.Length; j++)
				if (cycles[j] % cycles[i] == 0)
					counts[j] -= counts[i];

		Console.WriteLine(cycles.Zip(counts, (t, c) => t * c).Aggregate((u, v) => u + v).V);
	}
}

struct MInt
{
	const int M = 998244353;
	public long V;
	public MInt(long v) { V = (v %= M) < 0 ? v + M : v; }

	public static implicit operator MInt(long v) => new MInt(v);
	public static MInt operator +(MInt x, MInt y) => x.V + y.V;
	public static MInt operator -(MInt x, MInt y) => x.V - y.V;
	public static MInt operator *(MInt x, MInt y) => x.V * y.V;
}