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: Accepted)

N = 998244353

def calc_divisors(n)
  divisors = []
  1.upto(Math.sqrt(n).to_i) do |k|
    if n % k == 0
      divisors << k
      divisors << n / k
    end
  end
  divisors.uniq.sort
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_i = x_s.to_i(2)
  ks = calc_ks(n)
  c = 0
  ans = 0
  pairs = []
  ks.each do |k|
    s = x_s[0...k]
    s_rev = s.chars.map {|c| c == "0" ? "1" : "0" }.join
    s_n = s.to_i(2)
    y_s = s + (s_rev + s) * ((n/k - 1)/2)
    y_i = y_s.to_i(2)
    s_n += 1 if (y_i <= x_i)
    pairs.each do |pair|
      s_n -= pair[1] if k % pair[0] == 0
    end
    pairs << [k, s_n]
    c += s_n
    ans += (2 * k) * s_n
    ans = ans % N
  end
  ans += (x_i%N + 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_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