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 Rust (1.15.1) (judge verdict: Accepted)

const MOD: usize = 998244353;
fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let n: usize = sc.read();
    let x = sc
        .chars()
        .into_iter()
        .map(|c| if c == '0' { 0 } else { 1 })
        .collect::<Vec<_>>();

    let mut ans = vec![0; n * 2 + 1];

    for k in 2..(n * 2 + 1) {
        if (n * 2) % k != 0 || n % k == 0 {
            continue;
        }
        assert_eq!(k % 2, 0);
        let mut dp = [[0; 2]; 2];
        dp[0][0] = 1;
        for i in 0..(k / 2) {
            let x = x[i];
            let mut next = [[0; 2]; 2];
            for prev in 0..2 {
                for next_bit in 0..2 {
                    for below in 0..2 {
                        if below == 0 && next_bit > x {
                            continue;
                        }
                        let next_below = if next_bit < x { 1 } else { below };
                        next[next_below][next_bit] += dp[below][prev];
                        if next[next_below][next_bit] >= MOD {
                            next[next_below][next_bit] -= MOD;
                        }
                    }
                }
            }
            dp = next;
        }

        let below = (dp[1][0] + dp[1][1]) % MOD;
        let half = (dp[0][0] + dp[0][1]) % MOD;

        let mut ok = true;
        for i in 0..n {
            let t = i % (k / 2);
            let s = i % k;
            let y = if t == s { x[t] } else { x[t] ^ 1 };
            if x[i] < y {
                ok = false;
                break;
            }
            if x[i] > y {
                break;
            }
        }

        ans[k] = if ok { (below + half) % MOD } else { below };
    }

    for i in 1..(2 * n + 1) {
        let mut cur = 2 * i;
        while cur <= 2 * n {
            if ans[cur] > 0 {
                ans[cur] = (ans[cur] + MOD - ans[i]) % MOD;
            }
            cur += i;
        }
    }

    let ans = ans
        .into_iter()
        .enumerate()
        .fold(0, |ans, (i, v)| (ans + i * v) % MOD);
    println!("{}", ans);
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}