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

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

public static class P
{
    static int n;
    public static void Main()
    {
        n = int.Parse(Console.ReadLine());
        //n is odd : 10101010101
        //n is even : 1100110011
        string X = Console.ReadLine();
        int normalCircleFreq = 2 * n;

        var parsedX = Parse(X);
        var res = new ModInt(0);
        res += (parsedX + 1) * normalCircleFreq;
        

        List<int> factors = new List<int>();
        var sqrt = (int)Ceiling(Sqrt(n));
        for (int i = 1; i <= sqrt; i++)
        {
            if (n % i == 0)
            {
                factors.Add(i);
                factors.Add(n / i);
            }
        }

        ModInt[] f = new ModInt[n];
        var specialCountCands = factors.Distinct().OrderByDescending(x => x).Where(x => (x & 1) == 1).ToArray();
        foreach (var specialCount in specialCountCands)
        {
            var blockLen = n / specialCount;
            var cand = X.Substring(0, blockLen);

            var special = MakeSpecial(X.Substring(0, blockLen));
            bool isLargerThanSpecial = X.CompareTo(special) >= 0;
            
            int specialCircleFreq = blockLen * 2;
            var parsedCand = Parse(cand);
            var count = (parsedCand + (isLargerThanSpecial ? 1 : 0));
            res -= count * (normalCircleFreq - specialCircleFreq);

            for (int i = 3; i <= specialCount; i += 2)
            {
                //blockをi個集めてから
                if (specialCount % i != 0) continue;
                //要は今回のやつの個数は 普通に含まれてるので
                var freq = specialCircleFreq * i;
                res += count * (normalCircleFreq - freq);
            }
        }
        Console.WriteLine(res);
    }

    static string MakeSpecial(string block)
    {
        var blockCount = n / block.Length;
        var rev = string.Join("", block.Select(x => x == '0' ? '1' : '0'));
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < blockCount; i++)
        {
            if (i % 2 == 0) builder.Append(block);
            else builder.Append(rev);
        }
        return builder.ToString();
    }

    static string Decr(string s)
    {
        bool carry = true;
        char[] res = new char[s.Length];
        for (int i = s.Length - 1; i >= 0; i--)
        {
            if (carry)
            {
                if (s[i] == '1') carry = false;
                res[i] = s[i] == '0' ? '1' : '0';
            }
            else res[i] = s[i];
        }
        return string.Join("", res);
    }

    static ModInt Parse(string s)
    {
        ModInt curDig = 1;
        ModInt res = 0;
        foreach (var c in s.Reverse())
        {
            if (c == '1') res += curDig;
            curDig *= 2;
        }
        return res;
    }

    public static int Next(this int i) => i / 2 + (i % 2 == 0 ? 1 << (n - 1) : 0);
}

struct ModInt
{
    const int MOD = 998244353;
    const long POSITIVIZER = ((long)MOD) << 31;
    long Data;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public ModInt(long data) { if ((Data = data % MOD) < 0) Data += MOD; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator long(ModInt modInt) => modInt.Data;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator ModInt(long val) => new ModInt(val);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt a, int b) => new ModInt() { Data = (a.Data + b + POSITIVIZER) % MOD };
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt a, long b) => new ModInt(a.Data + b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt a, ModInt b) { long res = a.Data + b.Data; return new ModInt() { Data = res >= MOD ? res - MOD : res }; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator -(ModInt a, int b) => new ModInt() { Data = (a.Data - b + POSITIVIZER) % MOD };
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator -(ModInt a, long b) => new ModInt(a.Data - b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator -(ModInt a, ModInt b) { long res = a.Data - b.Data; return new ModInt() { Data = res < 0 ? res + MOD : res }; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt a, int b) => new ModInt(a.Data * b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt a, long b) => a * new ModInt(b);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt a, ModInt b) => new ModInt() { Data = a.Data * b.Data % MOD };
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator /(ModInt a, ModInt b) => new ModInt() { Data = a.Data * GetInverse(b) % MOD };
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public override string ToString() => Data.ToString();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static long GetInverse(long a)
    {
        long div, p = MOD, x1 = 1, y1 = 0, x2 = 0, y2 = 1;
        while (true)
        {
            if (p == 1) return x2 + MOD; div = a / p; x1 -= x2 * div; y1 -= y2 * div; a %= p;
            if (a == 1) return x1 + MOD; div = p / a; x2 -= x1 * div; y2 -= y1 * div; p %= a;
        }
    }
}