Language: C++

#ifndef __INTMOD_H__
#define __INTMOD_H__

/* updated: 2019-10-03 */

#include <iostream>
#include <cassert>
#include <vector>
#include <cstdint>

template <uint32_t Mod>
class IntMod {
	static_assert(Mod != 0, "Mod must not be 0");
	static_assert(Mod < 0x80000000, "Mod must be less than 0x80000000");

private:
	uint32_t value_m;

public:
	IntMod();
	IntMod(int32_t value);
	IntMod(uint32_t value);
	IntMod(int64_t value);
	IntMod(uint64_t value);

	const IntMod<Mod>& operator+() const;
	IntMod<Mod> operator-() const;
	IntMod<Mod>& operator++();
	IntMod<Mod>& operator--();
	IntMod<Mod> operator++(int dummy);
	IntMod<Mod> operator--(int dummy);
	IntMod<Mod>& operator+=(const IntMod<Mod>& right);
	IntMod<Mod>& operator-=(const IntMod<Mod>& right);
	IntMod<Mod>& operator*=(const IntMod<Mod>& right);
	IntMod<Mod>& operator/=(const IntMod<Mod>& right);
	IntMod<Mod> operator[](uint64_t exp) const;

	static IntMod<Mod> add(const IntMod<Mod>& a, const IntMod<Mod>& b);
	static IntMod<Mod> subtract(const IntMod<Mod>& a, const IntMod<Mod>& b);
	static IntMod<Mod> multiply(const IntMod<Mod>& a, const IntMod<Mod>& b);
	static IntMod<Mod> divide(const IntMod<Mod>& a, const IntMod<Mod>& b);
	static int compare(const IntMod<Mod>& a, const IntMod<Mod>& b);

	IntMod<Mod> inverse() const;
	IntMod<Mod> power(uint64_t exp) const;
	uint32_t get() const;

	static IntMod<Mod> fact(uint32_t num);
	static IntMod<Mod> combi(uint32_t n, uint32_t r);
	static IntMod<Mod> perm(uint32_t n, uint32_t r);
	static std::vector<IntMod<Mod>> calcInverseList(int max);
};

template <uint32_t Mod>
IntMod<Mod> operator+(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
IntMod<Mod> operator-(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
IntMod<Mod> operator*(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
IntMod<Mod> operator/(const IntMod<Mod>& a, const IntMod<Mod>& b);

template <uint32_t Mod, class Int>
IntMod<Mod> operator+(const IntMod<Mod>& a, const Int& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator-(const IntMod<Mod>& a, const Int& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator*(const IntMod<Mod>& a, const Int& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator/(const IntMod<Mod>& a, const Int& b);

template <uint32_t Mod, class Int>
IntMod<Mod> operator+(const Int& a, const IntMod<Mod>& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator-(const Int& a, const IntMod<Mod>& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator*(const Int& a, const IntMod<Mod>& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator/(const Int& a, const IntMod<Mod>& b);

template <uint32_t Mod>
bool operator==(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator!=(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator<(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator<=(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator>(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator>=(const IntMod<Mod>& a, const IntMod<Mod>& b);

template <uint32_t Mod, class Int>
bool operator==(const IntMod<Mod>& a, const Int& b);
template <uint32_t Mod, class Int>
bool operator!=(const IntMod<Mod>& a, const Int& b);

template <uint32_t Mod>
std::istream& operator>>(std::istream& ist, IntMod<Mod>& object);
template <uint32_t Mod>
std::ostream& operator<<(std::ostream& ost, const IntMod<Mod>& object);

using MInt = IntMod<1000000007>;

/*------------------------------------*/
/*--------   implementation   --------*/
/*------------------------------------*/

template <uint32_t Mod>
inline IntMod<Mod>::IntMod() 
	: value_m(0) {
}

template <uint32_t Mod>
inline IntMod<Mod>::IntMod(int32_t value) {
	int32_t tmp = value % int32_t(Mod);
	value_m = tmp >= 0 ? tmp : Mod - uint32_t(-tmp);
}

template <uint32_t Mod>
inline IntMod<Mod>::IntMod(uint32_t value)
	: value_m(value % Mod) {
}

template <uint32_t Mod>
inline IntMod<Mod>::IntMod(int64_t value) {
	int32_t tmp = value % int64_t(Mod);
	value_m = tmp >= 0 ? tmp : Mod - uint32_t(-tmp);
}

template <uint32_t Mod>
inline IntMod<Mod>::IntMod(uint64_t value)
	: value_m(value % Mod) {
}

template <uint32_t Mod>
inline const IntMod<Mod>& IntMod<Mod>::operator+() const {
	return *this;
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::operator-() const {
	IntMod<Mod> ret;
	if (value_m == 0) {
		ret.value_m = 0;
	} else {
		ret.value_m = Mod - value_m;
	}
	return ret;
}

template <uint32_t Mod>
inline IntMod<Mod>& IntMod<Mod>::operator++() {
	++value_m;
	if (value_m == Mod) {
		value_m = 0;
	}
	return *this;
}

template <uint32_t Mod>
inline IntMod<Mod>& IntMod<Mod>::operator--() {
	if (value_m == 0) {
		value_m = Mod;
	}
	--value_m;
	return *this;
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::operator++(int dummy) {
	IntMod ret(*this);
	++*this;
	return ret;
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::operator--(int dummy) {
	IntMod ret(*this);
	--*this;
	return ret;
}

template <uint32_t Mod>
inline IntMod<Mod>& IntMod<Mod>::operator+=(const IntMod<Mod>& right) {
	value_m += right.value_m;		// note: value_m < 0x80000000
	if (value_m >= Mod) {
		value_m -= Mod;
	}
	return *this;
}

template <uint32_t Mod>
inline IntMod<Mod>& IntMod<Mod>::operator-=(const IntMod<Mod>& right) {
	if (value_m < right.value_m) {
		value_m += Mod;				// note: value_m < 0x80000000
	}
	value_m -= right.value_m;
	return *this;
}

template <uint32_t Mod>
inline IntMod<Mod>& IntMod<Mod>::operator*=(const IntMod<Mod>& right) {
	value_m = (uint64_t(value_m) * right.value_m) % Mod;
	return *this;
}

template <uint32_t Mod>
inline IntMod<Mod>& IntMod<Mod>::operator/=(const IntMod<Mod>& right) {
	*this *= right.inverse();
	return *this;
}

// for power
template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::operator[](uint64_t exp) const {
	return power(exp);
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::add(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	IntMod<Mod> ret(a);
	ret += b;
	return ret;
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::subtract(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	IntMod<Mod> ret(a);
	ret -= b;
	return ret;
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::multiply(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	IntMod<Mod> ret(a);
	ret *= b;
	return ret;
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::divide(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	IntMod<Mod> ret(a);
	ret /= b;
	return ret;
}

template <uint32_t Mod>
inline int IntMod<Mod>::compare(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	if (a.value_m < b.value_m) return -1;
	if (a.value_m > b.value_m) return 1;
	return 0;
}

/* Mod must be a prime. */
template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::inverse() const {
	assert(value_m != 0);
	return power(Mod - 2);
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::power(uint64_t exp) const {
	IntMod product(1);
	IntMod factor(*this);
	while (exp > 0) {
		if (exp& 1) {
			product *= factor;
		}
		factor *= factor;
		exp >>= 1;
	}
	return product;
}

template <uint32_t Mod>
inline uint32_t IntMod<Mod>::get() const {
	return value_m;
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::fact(uint32_t num) {
	assert(num <= 0x80000000);

	static std::vector<IntMod<Mod>> table(1, 1);
	if (table.size() > num) return table[num];

	int old_size = table.size();
	table.resize(num + 1);
	for (int i = old_size; i <= num; ++i) {
		table[i] = table[i - 1] * i;
	}
	return table[num];
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::combi(uint32_t n, uint32_t r) {
	assert(n >= r);
	return fact(n) / (fact(n - r) * fact(r));
}

template <uint32_t Mod>
inline IntMod<Mod> IntMod<Mod>::perm(uint32_t n, uint32_t r) {
	assert(n >= r);
	return fact(n) / fact(n - r);
}

template <uint32_t Mod>
inline std::vector<IntMod<Mod>> IntMod<Mod>::calcInverseList(int max) {
	assert(max < Mod);

	std::vector<IntMod> table(max + 1);
	table[1] = 1;
	for (int i = 2; i <= max; ++i) {
		table[i] = (uint64_t(Mod - Mod / i) * table[Mod % i].Get()) % Mod;
	}
	return table;
}

template <uint32_t Mod>
inline IntMod<Mod> operator+(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::add(a, b);
}

template <uint32_t Mod>
inline IntMod<Mod> operator-(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::subtract(a, b);
}

template <uint32_t Mod>
inline IntMod<Mod> operator*(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::multiply(a, b);
}

template <uint32_t Mod>
inline IntMod<Mod> operator/(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::divide(a, b);
}

template<uint32_t Mod, class Int>
inline IntMod<Mod> operator+(const IntMod<Mod>& a, const Int& b) {
	return a + IntMod<Mod>(b);
}

template<uint32_t Mod, class Int>
inline IntMod<Mod> operator-(const IntMod<Mod>& a, const Int& b) {
	return a - IntMod<Mod>(b);
}

template<uint32_t Mod, class Int>
inline IntMod<Mod> operator*(const IntMod<Mod>& a, const Int& b) {
	return a * IntMod<Mod>(b);
}

template<uint32_t Mod, class Int>
inline IntMod<Mod> operator/(const IntMod<Mod>& a, const Int& b) {
	return a / IntMod<Mod>(b);
}

template<uint32_t Mod, class Int>
inline IntMod<Mod> operator+(const Int& a, const IntMod<Mod>& b) {
	return IntMod<Mod>(a) + b;
}

template<uint32_t Mod, class Int>
inline IntMod<Mod> operator-(const Int& a, const IntMod<Mod>& b) {
	return IntMod<Mod>(a) - b;
}

template<uint32_t Mod, class Int>
inline IntMod<Mod> operator*(const Int& a, const IntMod<Mod>& b) {
	return IntMod<Mod>(a) * b;
}

template<uint32_t Mod, class Int>
inline IntMod<Mod> operator/(const Int& a, const IntMod<Mod>& b) {
	return IntMod<Mod>(a) / b;
}

template <uint32_t Mod>
inline bool operator==(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) == 0;
}

template <uint32_t Mod>
inline bool operator!=(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) != 0;
}

template <uint32_t Mod>
inline bool operator<(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) < 0;
}

template <uint32_t Mod>
inline bool operator<=(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) <= 0;
}

template <uint32_t Mod>
inline bool operator>(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) > 0;
}

template <uint32_t Mod>
inline bool operator>=(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) >= 0;
}

template <uint32_t Mod, class Int>
bool operator==(const IntMod<Mod>& a, const Int& b) {
	return a == IntMod<Mod>(b);
}

template <uint32_t Mod, class Int>
bool operator!=(const IntMod<Mod>& a, const Int& b) {
	return a != IntMod<Mod>(b);
}

template <uint32_t Mod>
inline std::istream& operator>>(std::istream& ist, IntMod<Mod>& object) {
	uint64_t value;
	ist >> value;
	object = IntMod<Mod>(value);
	return ist;
}

template <uint32_t Mod>
inline std::ostream& operator<<(std::ostream& ost, const IntMod<Mod>& object) {
	ost << object.get();
	return ost;
}

#endif

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using uint = unsigned int;
using LL = long long;
using ULL = unsigned long long;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##min = (a); i >= i##min; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "BinaryMatrix.h"
#include "BIT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "LazySegmentTree.h"
#include "Math.h"
#include "Matrix.h"
#include "MinMax.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "SuffixArray.h"
#include "Tree.h"
#include "UnionFind.h"
#include "MinCostFlowSolver.h"
#include "Bipartite.h"
#include "FFT.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>> (istream& ist, __int128& val) { LL tmp;  ist >> tmp; val = tmp; return ist; }
ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

using MM = IntMod<998244353>;

int N;
string S;
vector<int> divisors(int n) {
	vector<int> ret;
	for (int i = 2; i * i <= n; ++i) {
		if (n % i == 0) {
			ret.push_back(i);
		}
		while (n % i == 0) {
			n /= i;
		}
	}

	if (n > 1) {
		ret.push_back(n);
	}
	return ret;
}

map<int, MM> moeb(int n) {
	auto divs = divisors(n);
	int k = divs.size();

	map<int, MM> moe;
	REP(s, 0, 1 << k) {
		int prod = 1;
		int val = 1;
		REP(i, 0, k) {
			if (s & (1 << i)) {
				prod *= divs[i];
				val = -val;
			}
		}
		moe[prod] = val;
	}
	return moe;
}

MM sub(int k) {
	string T = S.substr(0, k);
	MM sum = 0;
	REP(i, 0, k) {
		sum *= 2;
		if (T[i] == '1') ++sum;
	}

	string Tp = T;
	for (auto&& c : Tp) {
		if (c == '1') {
			c = '0';
		} else {
			c = '1';
		}
	}

	string U;
	REP(z, 0, N / k) {
		U += T;
		U += Tp;
	}
	if (U <= S) ++sum;
	return sum;
}

MM A[400001];
MM B[400001];
int main() {
	cin >> N >> S;
	REP(i, 0, N) {
		S += S[i] ^ 1;
	}

	REP(k, 1, 2 * N + 1) {
		if (2 * N % k != 0) continue;
		int d = 2 * N / k;
		if (d % 2 == 0) continue;
		A[k] = sub(k / 2);
	}

	auto mp = moeb(2 * N);
	MM sum = 0;
	REP(k, 1, 2 * N + 1) {
		if (2 * N % k != 0) continue;
		for (auto p : mp) {
			int d = p.first;
			MM coeff = p.second;
			if (k % d != 0) continue;
			B[k] += coeff * A[k / d];
		}
		sum += B[k] * k;
	}
	cout << sum << endl;
	return 0;
}



Compiler output:
a.cc: In instantiation of 'IntMod<Mod> operator*(const IntMod<Mod>&, const Int&) [with unsigned int Mod = 998244353; Int = long long int]':
a.cc:613:17:   required from here
  613 |                 sum += B[k] * k;
      |                               ^
a.cc:353:20: error: call of overloaded 'IntMod(const long long int&)' is ambiguous
  353 |         return a * IntMod<Mod>(b);
      |                    ^~~~~~~~~~~~~~
a.cc:133:8: note: candidate: 'IntMod<Mod>::IntMod(uint64_t) [with unsigned int Mod = 998244353; uint64_t = long unsigned int]'
  133 | inline IntMod<Mod>::IntMod(uint64_t value)
      |        ^~~~~~~~~~~
a.cc:127:8: note: candidate: 'IntMod<Mod>::IntMod(int64_t) [with unsigned int Mod = 998244353; int64_t = long int]'
  127 | inline IntMod<Mod>::IntMod(int64_t value) {
      |        ^~~~~~~~~~~
a.cc:122:8: note: candidate: 'IntMod<Mod>::IntMod(uint32_t) [with unsigned int Mod = 998244353; uint32_t = unsigned int]'
  122 | inline IntMod<Mod>::IntMod(uint32_t value)
      |        ^~~~~~~~~~~
a.cc:116:8: note: candidate: 'IntMod<Mod>::IntMod(int32_t) [with unsigned int Mod = 998244353; int32_t = int]'
  116 | inline IntMod<Mod>::IntMod(int32_t value) {
      |        ^~~~~~~~~~~
a.cc:12:7: note: candidate: 'constexpr IntMod<998244353>::IntMod(const IntMod<998244353>&)'
   12 | class IntMod {
      |       ^~~~~~
a.cc:12:7: note: candidate: 'constexpr IntMod<998244353>::IntMod(IntMod<998244353>&&)'