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)

// https://atcoder.jp/contests/agc039/tasks/agc039_c
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn rotate(mut a: Vec<char>) -> Vec<char> {
    let n = a.len();
    let tmp = a[n-1];
    for i in (1..n).rev() {
        a[i] = a[i-1];
    }
    a[0] = ifv!(tmp == '0', '1', '0');
    a
}

fn check(mut a: Vec<char>) -> usize {
    let mut b = a.clone();
    let mut cnt = 0;
    while true {
        a = rotate(a);
        cnt += 1;
        if a == b {
            return cnt
        }
    }
    0
}

const MOD: i64 = 998244353;

fn count_less(x: &Vec<char>) -> i64 {
    let n = x.len();
    let mut ret = 0;
    for i in 0..n {
        ret = ret * 2 + ifv!(x[i] == '1', 1, 0);
        ret %= MOD;
    }
    (ret + 1) % MOD
}

fn count_ptn(x: &Vec<char>, gl: usize) -> i64 {
    let mut dp = dvec!(0; gl+1, 2);
    dp[0][0] = 1;
    for idx in 0..gl {
        for f in 0..2 {
            let mut base = dp[idx][f];
            for c in 0..2 {
                let mut tf = f;
                if c == 0 && x[idx] == '1' {
                    tf = 1;
                }
                if f == 0 && x[idx] == '0' && c == 1 {
                    continue;
                }
                dp[idx+1][tf] += base;
                dp[idx+1][tf] %= MOD;
            }
        }
    }

    let n = x.len();
    let mut big = false;
    for i in 0..n {
        let c = i / gl;
        let mut wo = x[i%gl];
        if c % 2 == 1 {
            wo = ifv!(wo == '1', '0', '1');
        }
        if wo == '1' && x[i] == '0' {
            big = true;
            break;
        } else if wo == '0' && x[i] == '1' {
            break;
        }
    }

    (dp[gl][1] + ifv!(big, 0, 1)) % MOD
}

fn main() {
    input! {
        n: usize,
        x: chars
    };


    let mut wp = 1;
    {
        let mut n = n;
        while n % 2 == 0 {
            wp *= 2;
            n /= 2;
        }
    }
    let cnt = count_less(&x);


    let mut wset = vec![];
    for l in (1..(n+1)).rev() {
        if l % 2 == 0 {
            continue;
        }
        if n % l != 0 {
            continue;
        }

        let co = count_ptn(&x, n/l);
        wset.push(vec![l as i64, co]);
    }


    // debug!(wset);

    let mut special = 0;
    let mut wn = wset.len();
    for i in 0..wn {
        let mut dec = 0;
        for j in 0..i {
            if wset[j][0] % wset[i][0] == 0 {
                dec += wset[j][1];
                dec %= MOD;
            }
        }

        wset[i][1] = (wset[i][1] + MOD - dec) % MOD;
        let mut co = wset[i][1];
        let mut k = (n as i64)/wset[i][0];
        // debug!(co, dec, k);
        special += (k * 2) % MOD * co % MOD;
        special %= MOD;
    }

    println!("{}", special);
}