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 C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

template <std::uint_fast64_t Modulus>
class modint {
  using u64 = std::uint_fast64_t;

 public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept
      : a(x % Modulus) {
  }
  constexpr u64 &value() noexcept {
    return a;
  }
  constexpr const u64 &value() const noexcept {
    return a;
  }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(const modint rhs) noexcept {
    return *this *= ~rhs;
  }
  constexpr modint power(u64 exp) const noexcept {
    modint v = 1, x = *this;
    while (exp) {
      if (exp & 1) {
        v *= x;
      }
      x *= x;
      exp >>= 1;
    }
    return v;
  }
  constexpr modint operator~() const noexcept {
    return power(Modulus - 2);
  }
};

vector<int> divisors(int N) {
  vector<int> res;
  for (int i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      res.push_back(i);
      if (N / i != i) res.push_back(N / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

using mint = modint<998244353>;

struct edge_t {
  int to;
  int w;
};

mint pw2[200000 + 10];

mint solve(int N, const vector<int>& X, int L) {
  vec<edge_t, 2> G(N);
  for (int i = 0; i < N; i++) {
    int j = (i + L) % N;
    int w = ((i + L) / N) % 2;
    G[i].push_back({ j, w });
  }
  vector<int> id(N, -1), color(N, -1);
  vec<int, 2> group;
  for (int i = 0; i < N; i++) {
    if (id[i] != -1) continue;
    int v = i, c = 0, k = group.size();
    group.push_back({});
    while (id[v] == -1) {
      id[v] = k;
      color[v] = c;
      group[k].push_back(v);
      assert(G[v].size() == 1);
      int nv = G[v][0].to;
      int w = G[v][0].w;
      v = nv;
      c ^= w;
    }
    assert(v == i);
    if (c) return 0;
  }
  mint res = 0;
  int rem = group.size();
  vector<int> bit(N, -1);
  for (int i = N - 1; i >= 0; i--) {
    if (bit[i] != -1) {
      if (bit[i] > X[i]) {
        return res;
      }
      if (bit[i] < X[i]) {
        res += pw2[rem];
        return res;
      }
      continue;
    }
    int k = id[i];
    --rem;
    if (X[i] == 0) {
      for (int v : group[k]) bit[v] = (0 ^ color[i] ^ color[v]);
    } else {
      res += pw2[rem];
      for (int v : group[k]) bit[v] = (1 ^ color[i] ^ color[v]);
    }
  }
  res += 1;
  return res;
}

int main() {
#ifdef DEBUG
  ifstream ifs("in.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
  int N;
  string S;
  while (cin >> N >> S) {
    pw2[0] = 1;
    for (int i = 1; i <= N; i++) pw2[i] = pw2[i - 1] * 2;
    reverse(S.begin(), S.end());
    vector<int> X(N);
    for (int i = 0; i < N; i++) X[i] = S[i] - '0';
    auto divs = divisors(2 * N);
    vector<mint> ways(divs.size());
    for (int i = 0; i < divs.size(); i++) ways[i] = solve(N, X, divs[i]);
    for (int i = 0; i < divs.size(); i++) {
      for (int j = 0; j < i; j++) {
        if (divs[i] % divs[j] == 0) ways[i] -= ways[j];
      }
    } 
    mint res = 0;
    for (int i = 0; i < divs.size(); i++) {
      res += ways[i] * divs[i];
    }
    cout << res.value() << endl;
  }
  return 0;
}