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 Go (1.6) (judge verdict: Accepted)

package main
import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
)

const p = 998244353

func Solve() {
  var N int
  NextInt(&N)
  X := make([]int, N)
  for i, x := range NextLine() {
    if x == '1' { X[i]++ }
  }
  B := make([]int, N + 1)
  B[1] = 1
  for i := range B[2:] { B[i + 2] = 2 * B[i + 1] % p }
  F := func(n int) int {
    A := 0
    for i := n - 1; 0 <= i; i-- {
      A = (A + B[n - i] * X[i]) % p
    }
    return A
  }
  LE := func(Y []int) bool {
    for i := 0; i < N; i++ {
      if X[i] != Y[i] { return Y[i] < X[i] }
    }
    return true
  }
  ans := 2 * N * (F(N) + 1) % p
  D := make([]int, 0)
  for k := 1; 3 * k <= N; k++ {
    if N % (2 * k) == k { D = append(D, k) }
  }
  C := make([]int, len(D))
  Y := make([]int, N)
  for i, d := range D {
    C[i] = F(d)
    for i := range Y { Y[i] = X[i % d] ^ ((i / d) % 2) }
    if LE(Y) { C[i]++ }
    for j, d2 := range D[:i] {
      if d % d2 == 0 { C[i] = (C[i] - C[j] + p) % p }
    }
    ans = (ans - (2 * (N - d) * C[i] % p) + p) % p
  }
  Write(ans)
}

func main() {
  Solve()
  Output()
}

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt(A ...*int) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.Atoi(L[i])
  }
}
func NextIntVec(A *[]int) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]int, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.Atoi(l)
  }
}
func NextFloat(A ...*float64) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.ParseFloat(L[i], 64)
  }
}
func NextFloatVec(A *[]float64) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]float64, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.ParseFloat(l, 64)
  }
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}
func MaxInt(A ...int) int {
  max := A[0]
  for _, a := range A {
    if max < a { max = a }
  }
  return max
}
func MinInt(A ...int) int {
  min := A[0]
  for _, a := range A {
    if a < min { min = a }
  }
  return min
}
func SumInt(A ...int) int {
  sum := 0
  for _, a := range A {
    sum += a
  }
  return sum
}
func AbsInt(x int) int {
  if x < 0 { x = -x }
  return x
}