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 Ruby (2.3.3) (judge verdict: Wrong Answer)

N = 998244353

def calc_divisors(n)
  divisors = []
  root_n = Math.sqrt(n).to_i
  1.upto(root_n) do |k|
    if n % k == 0
      divisors << k
      divisors << n / k
    end
  end
  divisors
end

def get_int
  gets.chop.to_i
end

def get_ints
  gets.chop.split.map(&:to_i)
end

def get_double
  gets.chop.to_f
end

def get_str
  gets.chop
end

def get_strs
  gets.chop.split
end

def main
  n = get_int
  x_s = get_str
  x = x_s.chars.map(&:to_i).reverse
  ks = calc_ks(n)
  c = 0
  ans = 0
  ks.each do |k|
    m = calc_smaller_num(k, n, x, x_s)
    c += m
    ans += (2 * k) * m
    ans = ans % N
  end
  ans += (calc_x_mod(x) + 1 - (c%N) + N) * (2 * n)
  ans = ans % N
  puts ans
end

def calc_ks(n)
  ks = []
  calc_divisors(n).each do |d|
    m = n / d
    if m >= 3 and m % 2 == 1
      ks << d
    end
  end
  ks
end

def calc_smaller_num(k, n, x, x_s)
  s = x[(-k)..(-1)].reverse.map(&:to_s).join
  s_n = s.to_i(2)
  smaller = s_n

  s_rev = s.chars.map {|c| c == "0" ? "1" : "0" }.join
  sub_s = s_rev + s
  is_small = true
  k.upto(n - 1) do |i|
    x_c = x_s[i]
    y_c = sub_s[(i-k)%(2*k)]
    break if x_c > y_c
    if y_c > x_c
      is_small = false
      break
    end
  end
  smaller += 1 if is_small
  smaller
end

def calc_x_mod(x)
  m = 0
  x.each_with_index do |b, i|
    next if b == 0
    l = 1
    i.times do
      l = l * 2
      l = l % N
    end
    m += l
    m = m % N
  end
  m
end

if __FILE__ == $0
  main
end