using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace YukiCoder
{
public class Program
{
static void Main()
{
using var cin = new Scanner();
var (n, q) = cin.Int2();
var a = cin.ArrayLong(n);
var b = new long[n + 2];
for (int i = 0; i < q; i++) {
int r = cin.Int();
b[(n - r) % n]++;
}
var conv = Math.ConvolutionLong(a, b);
var ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] += conv[i];
ret[i] += conv[i + n];
}
Console.WriteLine(ret.Join(" "));
}
}
public static partial class Math
{
///
/// 畳み込みを mod = 998244353 で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static int[] Convolution(int[] a, int[] b) => Convolution(a, b);
///
/// 畳み込みを mod = 998244353 で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static uint[] Convolution(uint[] a, uint[] b) => Convolution(a, b);
///
/// 畳み込みを mod = 998244353 で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static long[] Convolution(long[] a, long[] b) => Convolution(a, b);
///
/// 畳み込みを mod = 998244353 で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static ulong[] Convolution(ulong[] a, ulong[] b) => Convolution(a, b);
///
/// 畳み込みを mod で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static int[] Convolution(int[] a, int[] b) where TMod : struct, IStaticMod
{
var n = a.Length;
var m = b.Length;
if (n == 0 || m == 0) {
return Array.Empty();
}
if (System.Math.Min(n, m) <= 60) {
var c = ConvolutionNaive(a.Select(ai => new StaticModInt(ai)).ToArray(),
b.Select(bi => new StaticModInt(bi)).ToArray());
return c.Select(ci => ci.Value).ToArray();
} else {
int z = 1 << InternalMath.CeilPow2(n + m - 1);
var aTemp = new StaticModInt[z];
for (int i = 0; i < a.Length; i++) {
aTemp[i] = new StaticModInt(a[i]);
}
var bTemp = new StaticModInt[z];
for (int i = 0; i < b.Length; i++) {
bTemp[i] = new StaticModInt(b[i]);
}
var c = Convolution(aTemp, bTemp, n, m, z)[0..(n + m - 1)];
var result = new int[c.Length];
for (int i = 0; i < result.Length; i++) {
result[i] = c[i].Value;
}
return result;
}
}
///
/// 畳み込みを mod で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static uint[] Convolution(uint[] a, uint[] b) where TMod : struct, IStaticMod
{
var n = a.Length;
var m = b.Length;
if (n == 0 || m == 0) {
return Array.Empty();
}
if (System.Math.Min(n, m) <= 60) {
var c = ConvolutionNaive(a.Select(ai => new StaticModInt(ai)).ToArray(),
b.Select(bi => new StaticModInt(bi)).ToArray());
return c.Select(ci => (uint)ci.Value).ToArray();
} else {
int z = 1 << InternalMath.CeilPow2(n + m - 1);
var aTemp = new StaticModInt[z];
for (int i = 0; i < a.Length; i++) {
aTemp[i] = new StaticModInt(a[i]);
}
var bTemp = new StaticModInt[z];
for (int i = 0; i < b.Length; i++) {
bTemp[i] = new StaticModInt(b[i]);
}
var c = Convolution(aTemp, bTemp, n, m, z)[0..(n + m - 1)];
var result = new uint[c.Length];
for (int i = 0; i < result.Length; i++) {
result[i] = (uint)c[i].Value;
}
return result;
}
}
///
/// 畳み込みを mod で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static long[] Convolution(long[] a, long[] b) where TMod : struct, IStaticMod
{
var n = a.Length;
var m = b.Length;
if (n == 0 || m == 0) {
return Array.Empty();
}
if (System.Math.Min(n, m) <= 60) {
var c = ConvolutionNaive(a.Select(ai => new StaticModInt(ai)).ToArray(),
b.Select(bi => new StaticModInt(bi)).ToArray());
return c.Select(ci => (long)ci.Value).ToArray();
} else {
int z = 1 << InternalMath.CeilPow2(n + m - 1);
var aTemp = new StaticModInt[z];
for (int i = 0; i < a.Length; i++) {
aTemp[i] = new StaticModInt(a[i]);
}
var bTemp = new StaticModInt[z];
for (int i = 0; i < b.Length; i++) {
bTemp[i] = new StaticModInt(b[i]);
}
var c = Convolution(aTemp, bTemp, n, m, z)[0..(n + m - 1)];
var result = new long[c.Length];
for (int i = 0; i < result.Length; i++) {
result[i] = c[i].Value;
}
return result;
}
}
///
/// 畳み込みを mod で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static ulong[] Convolution(ulong[] a, ulong[] b) where TMod : struct, IStaticMod
{
var n = a.Length;
var m = b.Length;
if (n == 0 || m == 0) {
return Array.Empty();
}
if (System.Math.Min(n, m) <= 60) {
var c = ConvolutionNaive(a.Select(TakeMod).ToArray(),
b.Select(TakeMod).ToArray());
return c.Select(ci => (ulong)ci.Value).ToArray();
} else {
int z = 1 << InternalMath.CeilPow2(n + m - 1);
var aTemp = new StaticModInt[z];
for (int i = 0; i < a.Length; i++) {
aTemp[i] = TakeMod(a[i]);
}
var bTemp = new StaticModInt[z];
for (int i = 0; i < b.Length; i++) {
bTemp[i] = TakeMod(b[i]);
}
var c = Convolution(aTemp, bTemp, n, m, z)[0..(n + m - 1)];
var result = new ulong[c.Length];
for (int i = 0; i < result.Length; i++) {
result[i] = (ulong)c[i].Value;
}
return result;
}
StaticModInt TakeMod(ulong x) => StaticModInt.Raw((int)(x % default(TMod).Mod));
}
///
/// 畳み込みを mod で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static StaticModInt[] Convolution(StaticModInt[] a, StaticModInt[] b)
where TMod : struct, IStaticMod
{
var temp = Convolution((ReadOnlySpan>)a, b);
return temp.ToArray();
}
///
/// 畳み込みを mod で計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - 2≤≤2×10^9
/// - は素数
/// - 2^c | ( - 1) かつ || + || - 1 ≤ 2^c なる c が存在する
/// 計算量: O((||+||)log(||+||) + log)
///
public static Span> Convolution(ReadOnlySpan> a, ReadOnlySpan> b)
where TMod : struct, IStaticMod
{
var n = a.Length;
var m = b.Length;
if (n == 0 || m == 0) {
return Array.Empty>();
}
if (System.Math.Min(n, m) <= 60) {
return ConvolutionNaive(a, b);
}
int z = 1 << InternalMath.CeilPow2(n + m - 1);
var aTemp = new StaticModInt[z];
a.CopyTo(aTemp);
var bTemp = new StaticModInt[z];
b.CopyTo(bTemp);
return Convolution(aTemp.AsSpan(), bTemp.AsSpan(), n, m, z);
}
private static Span> Convolution(Span> a, Span> b, int n, int m, int z)
where TMod : struct, IStaticMod
{
Butterfly.Calculate(a);
Butterfly.Calculate(b);
for (int i = 0; i < a.Length; i++) {
a[i] *= b[i];
}
Butterfly.CalculateInv(a);
var result = a[0..(n + m - 1)];
var iz = new StaticModInt(z).Inv();
foreach (ref var r in result) {
r *= iz;
}
return result;
}
///
/// 畳み込みを計算します。
///
///
/// , の少なくとも一方が空の場合は空配列を返します。
/// 制約:
/// - || + || - 1 ≤ 2^24 = 16,777,216
/// - 畳み込んだ後の配列の要素が全て long に収まる
/// 計算量: O((||+||)log(||+||))
///
public static long[] ConvolutionLong(ReadOnlySpan a, ReadOnlySpan b)
{
unchecked {
var n = a.Length;
var m = b.Length;
if (n == 0 || m == 0) {
return Array.Empty();
}
const ulong Mod1 = 754974721;
const ulong Mod2 = 167772161;
const ulong Mod3 = 469762049;
const ulong M2M3 = Mod2 * Mod3;
const ulong M1M3 = Mod1 * Mod3;
const ulong M1M2 = Mod1 * Mod2;
// (m1 * m2 * m3) % 2^64
const ulong M1M2M3 = Mod1 * Mod2 * Mod3;
ulong i1 = (ulong)InternalMath.InvGCD((long)M2M3, (long)Mod1).Item2;
ulong i2 = (ulong)InternalMath.InvGCD((long)M1M3, (long)Mod2).Item2;
ulong i3 = (ulong)InternalMath.InvGCD((long)M1M2, (long)Mod3).Item2;
var c1 = Convolution(a, b);
var c2 = Convolution(a, b);
var c3 = Convolution(a, b);
var c = new long[n + m - 1];
Span offset = stackalloc ulong[] { 0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3 };
for (int i = 0; i < c.Length; i++) {
ulong x = 0;
x += (c1[i] * i1) % Mod1 * M2M3;
x += (c2[i] * i2) % Mod2 * M1M3;
x += (c3[i] * i3) % Mod3 * M1M2;
long diff = (long)c1[i] - InternalMath.SafeMod((long)x, (long)Mod1);
if (diff < 0) {
diff += (long)Mod1;
}
// 真値を r, 得られた値を x, M1M2M3 % 2^64 = M', B = 2^63 として、
// r = x,
// x - M' + (0 or 2B),
// x - 2M' + (0 or 2B or 4B),
// x - 3M' + (0 or 2B or 4B or 6B)
// のいずれかが成り立つ、らしい
// -> see atcoder/convolution.hpp
x -= offset[(int)(diff % offset.Length)];
c[i] = (long)x;
}
return c;
}
ulong[] Convolution(ReadOnlySpan a, ReadOnlySpan b) where TMod : struct, IStaticMod
{
int z = 1 << InternalMath.CeilPow2(a.Length + b.Length - 1);
var aTemp = new StaticModInt[z];
for (int i = 0; i < a.Length; i++) {
aTemp[i] = new StaticModInt(a[i]);
}
var bTemp = new StaticModInt[z];
for (int i = 0; i < b.Length; i++) {
bTemp[i] = new StaticModInt(b[i]);
}
var c = YukiCoder.Math.Convolution(aTemp, bTemp, a.Length, b.Length, z);
var result = new ulong[c.Length];
for (int i = 0; i < result.Length; i++) {
result[i] = (ulong)c[i].Value;
}
return result;
}
}
private static StaticModInt[] ConvolutionNaive(ReadOnlySpan> a, ReadOnlySpan> b)
where TMod : struct, IStaticMod
{
if (a.Length < b.Length) {
// ref 構造体のため型引数として使えない
var temp = a;
a = b;
b = temp;
}
var ans = new StaticModInt[a.Length + b.Length - 1];
for (int i = 0; i < a.Length; i++) {
for (int j = 0; j < b.Length; j++) {
ans[i + j] += a[i] * b[j];
}
}
return ans;
}
private readonly struct FFTMod1 : IStaticMod
{
public uint Mod => 754974721;
public bool IsPrime => true;
}
private readonly struct FFTMod2 : IStaticMod
{
public uint Mod => 167772161;
public bool IsPrime => true;
}
private readonly struct FFTMod3 : IStaticMod
{
public uint Mod => 469762049;
public bool IsPrime => true;
}
}
///
/// コンパイル時に決定する mod を表します。
///
///
///
/// public readonly struct Mod1000000009 : IStaticMod
/// {
/// public uint Mod => 1000000009;
/// public bool IsPrime => true;
/// }
///
///
public interface IStaticMod
{
///
/// mod を取得します。
///
uint Mod { get; }
///
/// mod が素数であるか識別します。
///
bool IsPrime { get; }
}
public readonly struct Mod1000000007 : IStaticMod
{
public uint Mod => 1000000007;
public bool IsPrime => true;
}
public readonly struct Mod998244353 : IStaticMod
{
public uint Mod => 998244353;
public bool IsPrime => true;
}
///
/// 実行時に決定する mod の ID を表します。
///
///
///
/// public readonly struct ModID123 : IDynamicModID { }
///
///
public interface IDynamicModID { }
public readonly struct ModID0 : IDynamicModID { }
public readonly struct ModID1 : IDynamicModID { }
public readonly struct ModID2 : IDynamicModID { }
///
/// 四則演算時に自動で mod を取る整数型。mod の値はコンパイル時に決定している必要があります。
///
/// 定数 mod を表す構造体
///
///
/// using ModInt = AtCoder.StaticModInt<AtCoder.Mod1000000007>;
///
/// void SomeMethod()
/// {
/// var m = new ModInt(1);
/// m -= 2;
/// Console.WriteLine(m); // 1000000006
/// }
///
///
public readonly struct StaticModInt where T : struct, IStaticMod
{
private readonly uint _v;
///
/// 格納されている値を返します。
///
public int Value => (int)_v;
///
/// mod を返します。
///
public static int Mod => (int)default(T).Mod;
///
/// に対して mod を取らずに StaticModInt<> 型のインスタンスを生成します。
///
///
/// 定数倍高速化のための関数です。 に 0 未満または mod 以上の値を入れた場合の挙動は未定義です。
/// 制約: 0≤||<mod
///
public static StaticModInt Raw(int v)
{
var u = unchecked((uint)v);
Debug.Assert(u < Mod);
return new StaticModInt(u);
}
///
/// StaticModInt<> 型のインスタンスを生成します。
///
///
/// が 0 未満、もしくは mod 以上の場合、自動で mod を取ります。
///
public StaticModInt(long v) : this(Round(v)) { }
private StaticModInt(uint v) => _v = v;
private static uint Round(long v)
{
var x = v % default(T).Mod;
if (x < 0) {
x += default(T).Mod;
}
return (uint)x;
}
public static StaticModInt operator ++(StaticModInt value)
{
var v = value._v + 1;
if (v == default(T).Mod) {
v = 0;
}
return new StaticModInt(v);
}
public static StaticModInt operator --(StaticModInt value)
{
var v = value._v;
if (v == 0) {
v = default(T).Mod;
}
return new StaticModInt(v - 1);
}
public static StaticModInt operator +(StaticModInt lhs, StaticModInt rhs)
{
var v = lhs._v + rhs._v;
if (v >= default(T).Mod) {
v -= default(T).Mod;
}
return new StaticModInt(v);
}
public static StaticModInt operator -(StaticModInt lhs, StaticModInt rhs)
{
unchecked {
var v = lhs._v - rhs._v;
if (v >= default(T).Mod) {
v += default(T).Mod;
}
return new StaticModInt(v);
}
}
public static StaticModInt operator *(StaticModInt lhs, StaticModInt rhs)
{
return new StaticModInt((uint)((ulong)lhs._v * rhs._v % default(T).Mod));
}
///
/// 除算を行います。
///
///
/// - 制約: に乗法の逆元が存在する。(gcd(, mod) = 1)
/// - 計算量: O(log(mod))
///
public static StaticModInt operator /(StaticModInt lhs, StaticModInt rhs) => lhs * rhs.Inv();
public static StaticModInt operator +(StaticModInt value) => value;
public static StaticModInt operator -(StaticModInt value) => new StaticModInt() - value;
public static bool operator ==(StaticModInt lhs, StaticModInt rhs) => lhs._v == rhs._v;
public static bool operator !=(StaticModInt lhs, StaticModInt rhs) => lhs._v != rhs._v;
public static implicit operator StaticModInt(int value) => new StaticModInt(value);
public static implicit operator StaticModInt(long value) => new StaticModInt(value);
///
/// 自身を x として、x^ を返します。
///
///
/// 制約: 0≤||
/// 計算量: O(log())
///
public StaticModInt Pow(long n)
{
Debug.Assert(0 <= n);
var x = this;
var r = new StaticModInt(1u);
while (n > 0) {
if ((n & 1) > 0) {
r *= x;
}
x *= x;
n >>= 1;
}
return r;
}
///
/// 自身を x として、 xy≡1 なる y を返します。
///
///
/// 制約: gcd(x, mod) = 1
///
public StaticModInt Inv()
{
if (default(T).IsPrime) {
Debug.Assert(_v > 0);
return Pow(default(T).Mod - 2);
} else {
var (g, x) = InternalMath.InvGCD(_v, default(T).Mod);
Debug.Assert(g == 1);
return new StaticModInt(x);
}
}
public override string ToString() => _v.ToString();
public override bool Equals(object obj) => obj is StaticModInt && this == (StaticModInt)obj;
public override int GetHashCode() => _v.GetHashCode();
}
///
/// 四則演算時に自動で mod を取る整数型。実行時に mod が決まる場合でも使用可能です。
///
///
/// 使用前に DynamicModInt<>.Mod に mod の値を設定する必要があります。
///
/// mod の ID を表す構造体
///
///
/// using AtCoder.ModInt = AtCoder.DynamicModInt<AtCoder.ModID0>;
///
/// void SomeMethod()
/// {
/// ModInt.Mod = 1000000009;
/// var m = new ModInt(1);
/// m -= 2;
/// Console.WriteLine(m); // 1000000008
/// }
///
///
public readonly struct DynamicModInt where T : struct, IDynamicModID
{
private readonly uint _v;
private static Barrett bt;
///
/// 格納されている値を返します。
///
public int Value => (int)_v;
///
/// mod を返します。
///
public static int Mod
{
get => (int)bt.Mod;
set
{
Debug.Assert(1 <= value);
bt = new Barrett((uint)value);
}
}
///
/// に対して mod を取らずに DynamicModInt<> 型のインスタンスを生成します。
///
///
/// 定数倍高速化のための関数です。 に 0 未満または mod 以上の値を入れた場合の挙動は未定義です。
/// 制約: 0≤||<mod
///
public static DynamicModInt Raw(int v)
{
var u = unchecked((uint)v);
Debug.Assert(bt != null, $"使用前に {nameof(DynamicModInt)}<{nameof(T)}>.{nameof(Mod)} プロパティに mod の値を設定してください。");
Debug.Assert(u < Mod);
return new DynamicModInt(u);
}
///
/// DynamicModInt<> 型のインスタンスを生成します。
///
///
/// - 使用前に DynamicModInt<>.Mod に mod の値を設定する必要があります。
/// - が 0 未満、もしくは mod 以上の場合、自動で mod を取ります。
///
public DynamicModInt(long v) : this(Round(v)) { }
private DynamicModInt(uint v) => _v = v;
private static uint Round(long v)
{
Debug.Assert(bt != null, $"使用前に {nameof(DynamicModInt)}<{nameof(T)}>.{nameof(Mod)} プロパティに mod の値を設定してください。");
var x = v % bt.Mod;
if (x < 0) {
x += bt.Mod;
}
return (uint)x;
}
public static DynamicModInt operator ++(DynamicModInt value)
{
var v = value._v + 1;
if (v == bt.Mod) {
v = 0;
}
return new DynamicModInt(v);
}
public static DynamicModInt operator --(DynamicModInt value)
{
var v = value._v;
if (v == 0) {
v = bt.Mod;
}
return new DynamicModInt(v - 1);
}
public static DynamicModInt operator +(DynamicModInt lhs, DynamicModInt rhs)
{
var v = lhs._v + rhs._v;
if (v >= bt.Mod) {
v -= bt.Mod;
}
return new DynamicModInt(v);
}
public static DynamicModInt operator -(DynamicModInt lhs, DynamicModInt rhs)
{
unchecked {
var v = lhs._v - rhs._v;
if (v >= bt.Mod) {
v += bt.Mod;
}
return new DynamicModInt(v);
}
}
public static DynamicModInt operator *(DynamicModInt lhs, DynamicModInt rhs)
{
uint z = bt.Mul(lhs._v, rhs._v);
return new DynamicModInt(z);
}
///
/// 除算を行います。
///
///
/// - 制約: に乗法の逆元が存在する。(gcd(, mod) = 1)
/// - 計算量: O(log(mod))
///
public static DynamicModInt operator /(DynamicModInt lhs, DynamicModInt rhs) => lhs * rhs.Inv();
public static DynamicModInt operator +(DynamicModInt value) => value;
public static DynamicModInt operator -(DynamicModInt value) => new DynamicModInt() - value;
public static bool operator ==(DynamicModInt lhs, DynamicModInt rhs) => lhs._v == rhs._v;
public static bool operator !=(DynamicModInt lhs, DynamicModInt rhs) => lhs._v != rhs._v;
public static implicit operator DynamicModInt(int value) => new DynamicModInt(value);
public static implicit operator DynamicModInt(long value) => new DynamicModInt(value);
///
/// 自身を x として、x^ を返します。
///
///
/// 制約: 0≤||
/// 計算量: O(log())
///
public DynamicModInt Pow(long n)
{
Debug.Assert(0 <= n);
var x = this;
var r = new DynamicModInt(1u);
while (n > 0) {
if ((n & 1) > 0) {
r *= x;
}
x *= x;
n >>= 1;
}
return r;
}
///
/// 自身を x として、 xy≡1 なる y を返します。
///
///
/// 制約: gcd(x, mod) = 1
///
public DynamicModInt Inv()
{
var (g, x) = InternalMath.InvGCD(_v, bt.Mod);
Debug.Assert(g == 1);
return new DynamicModInt(x);
}
public override string ToString() => _v.ToString();
public override bool Equals(object obj) => obj is DynamicModInt && this == (DynamicModInt)obj;
public override int GetHashCode() => _v.GetHashCode();
}
public static partial class InternalMath
{
private static readonly Dictionary primitiveRootsCache = new Dictionary()
{
{ 2, 1 },
{ 167772161, 3 },
{ 469762049, 3 },
{ 754974721, 11 },
{ 998244353, 3 }
};
///
/// の最小の原始根を求めます。
///
///
/// 制約: は素数
///
public static int PrimitiveRoot(int m)
{
Debug.Assert(m >= 2);
if (primitiveRootsCache.TryGetValue(m, out var p)) {
return p;
}
return primitiveRootsCache[m] = Calculate(m);
int Calculate(int m)
{
Span divs = stackalloc int[20];
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) {
x >>= 1;
}
for (int i = 3; (long)i * i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2; ; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (PowMod(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) {
return g;
}
}
}
}
///
/// ^ mod を返します。
///
///
/// 制約: 0≤, 1≤
/// 計算量: O(log)
///
public static long PowMod(long x, long n, int m)
{
Debug.Assert(0 <= n && 1 <= m);
if (m == 1) return 0;
Barrett barrett = new Barrett((uint)m);
uint r = 1, y = (uint)InternalMath.SafeMod(x, m);
while (0 < n) {
if ((n & 1) != 0) r = barrett.Mul(r, y);
y = barrett.Mul(y, y);
n >>= 1;
}
return r;
}
public static long SafeMod(long x, long m)
{
x %= m;
if (x < 0) x += m;
return x;
}
///
/// g=gcd(a,b),xa=g(mod b) となるような 0≤x<b/g の(g, x)
///
///
/// 制約: 1≤
///
public static (long, long) InvGCD(long a, long b)
{
a = SafeMod(a, b);
if (a == 0) return (b, 0);
long s = b, t = a;
long m0 = 0, m1 = 1;
long u;
while (true) {
if (t == 0) {
if (m0 < 0) m0 += b / s;
return (s, m0);
}
u = s / t;
s -= t * u;
m0 -= m1 * u;
if (s == 0) {
if (m1 < 0) m1 += b / t;
return (t, m1);
}
u = t / s;
t -= s * u;
m1 -= m0 * u;
}
}
///
/// ≤ 2**x を満たす最小のx
///
///
/// 制約: 0≤
///
public static int CeilPow2(int n)
{
var un = (uint)n;
if (un <= 1) return 0;
int ret = 0;
int pow = 1;
while (n > pow) {
++ret;
pow *= 2;
}
return ret;
}
}
///
/// Fast moduler by barrett reduction
///
///
public class Barrett
{
public uint Mod { get; private set; }
private ulong IM;
public Barrett(uint m)
{
Mod = m;
IM = unchecked((ulong)-1) / m + 1;
}
///
/// * mod m
///
public uint Mul(uint a, uint b)
{
ulong z = a;
z *= b;
return (uint)(z % Mod);
}
}
public static class Butterfly where T : struct, IStaticMod
{
///
/// sumE[i] = ies[0] * ... * ies[i - 1] * es[i]
///
private static StaticModInt[] sumE = CalcurateSumE();
///
/// sumIE[i] = es[0] * ... * es[i - 1] * ies[i]
///
private static StaticModInt[] sumIE = CalcurateSumIE();
public static void Calculate(Span> a)
{
var n = a.Length;
var h = InternalMath.CeilPow2(n);
for (int ph = 1; ph <= h; ph++) {
// ブロックサイズの半分
int w = 1 << (ph - 1);
// ブロック数
int p = 1 << (h - ph);
var now = StaticModInt.Raw(1);
// 各ブロックの s 段目
for (int s = 0; s < w; s++) {
int offset = s << (h - ph + 1);
for (int i = 0; i < p; i++) {
var l = a[i + offset];
var r = a[i + offset + p] * now;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
now *= sumE[InternalBit.BSF(~(uint)s)];
}
}
}
public static void CalculateInv(Span> a)
{
var n = a.Length;
var h = InternalMath.CeilPow2(n);
for (int ph = h; ph >= 1; ph--) {
// ブロックサイズの半分
int w = 1 << (ph - 1);
// ブロック数
int p = 1 << (h - ph);
var iNow = StaticModInt.Raw(1);
// 各ブロックの s 段目
for (int s = 0; s < w; s++) {
int offset = s << (h - ph + 1);
for (int i = 0; i < p; i++) {
var l = a[i + offset];
var r = a[i + offset + p];
a[i + offset] = l + r;
a[i + offset + p] = StaticModInt.Raw(
unchecked((int)((ulong)(default(T).Mod + l.Value - r.Value) * (ulong)iNow.Value % default(T).Mod)));
}
iNow *= sumIE[InternalBit.BSF(~(uint)s)];
}
}
}
private static StaticModInt[] CalcurateSumE()
{
int g = InternalMath.PrimitiveRoot((int)default(T).Mod);
int cnt2 = InternalBit.BSF(default(T).Mod - 1);
var e = new StaticModInt(g).Pow((default(T).Mod - 1) >> cnt2);
var ie = e.Inv();
var sumE = new StaticModInt[cnt2 - 2];
// es[i]^(2^(2+i)) == 1
Span> es = stackalloc StaticModInt[cnt2 - 1];
Span> ies = stackalloc StaticModInt[cnt2 - 1];
for (int i = es.Length - 1; i >= 0; i--) {
// e^(2^(2+i)) == 1
es[i] = e;
ies[i] = ie;
e *= e;
ie *= ie;
}
var now = StaticModInt.Raw(1);
for (int i = 0; i < sumE.Length; i++) {
sumE[i] = es[i] * now;
now *= ies[i];
}
return sumE;
}
private static StaticModInt[] CalcurateSumIE()
{
int g = InternalMath.PrimitiveRoot((int)default(T).Mod);
int cnt2 = InternalBit.BSF(default(T).Mod - 1);
var e = new StaticModInt(g).Pow((default(T).Mod - 1) >> cnt2);
var ie = e.Inv();
var sumIE = new StaticModInt[cnt2 - 2];
// es[i]^(2^(2+i)) == 1
Span> es = stackalloc StaticModInt[cnt2 - 1];
Span> ies = stackalloc StaticModInt[cnt2 - 1];
for (int i = es.Length - 1; i >= 0; i--) {
// e^(2^(2+i)) == 1
es[i] = e;
ies[i] = ie;
e *= e;
ie *= ie;
}
var now = StaticModInt.Raw(1);
for (int i = 0; i < sumIE.Length; i++) {
sumIE[i] = ies[i] * now;
now *= es[i];
}
return sumIE;
}
}
public static class InternalBit
{
///
/// _blsi_u32 OR & -
/// で立っているうちの最下位の 1 ビットのみを立てた整数を返す
///
///
/// & -
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ExtractLowestSetBit(int n)
{
return n & -n;
}
///
/// ( & (1 << x)) != 0 なる最小の非負整数 x を求めます。
///
///
/// BSF: Bit Scan Forward
/// 制約: 1 ≤
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int BSF(uint n)
{
for (int i = 0; i < 32; i++) {
if (((1 << i) & n) != 0) {
return i;
}
}
return 32;
}
}
public struct BitFlag
{
public static BitFlag Begin() => 0;
public static BitFlag End(int bitCount) => 1 << bitCount;
public static BitFlag FromBit(int bitNumber) => 1 << bitNumber;
private readonly int flags_;
public bool this[int bitNumber] => (flags_ & (1 << bitNumber)) != 0;
public BitFlag(int flags) { flags_ = flags; }
public bool Has(BitFlag target) => (flags_ & target.flags_) == target.flags_;
public bool Has(int target) => (flags_ & target) == target;
public bool HasBit(int bitNumber) => (flags_ & (1 << bitNumber)) != 0;
public BitFlag OrBit(int bitNumber) => (flags_ | (1 << bitNumber));
public BitFlag AndBit(int bitNumber) => (flags_ & (1 << bitNumber));
public BitFlag XorBit(int bitNumber) => (flags_ ^ (1 << bitNumber));
public static BitFlag operator ++(BitFlag src) => new BitFlag(src.flags_ + 1);
public static BitFlag operator --(BitFlag src) => new BitFlag(src.flags_ - 1);
public static BitFlag operator |(BitFlag lhs, BitFlag rhs)
=> new BitFlag(lhs.flags_ | rhs.flags_);
public static BitFlag operator |(BitFlag lhs, int rhs)
=> new BitFlag(lhs.flags_ | rhs);
public static BitFlag operator |(int lhs, BitFlag rhs)
=> new BitFlag(lhs | rhs.flags_);
public static BitFlag operator &(BitFlag lhs, BitFlag rhs)
=> new BitFlag(lhs.flags_ & rhs.flags_);
public static BitFlag operator &(BitFlag lhs, int rhs)
=> new BitFlag(lhs.flags_ & rhs);
public static BitFlag operator &(int lhs, BitFlag rhs)
=> new BitFlag(lhs & rhs.flags_);
public static bool operator <(BitFlag lhs, BitFlag rhs) => lhs.flags_ < rhs.flags_;
public static bool operator <(BitFlag lhs, int rhs) => lhs.flags_ < rhs;
public static bool operator <(int lhs, BitFlag rhs) => lhs < rhs.flags_;
public static bool operator >(BitFlag lhs, BitFlag rhs) => lhs.flags_ > rhs.flags_;
public static bool operator >(BitFlag lhs, int rhs) => lhs.flags_ > rhs;
public static bool operator >(int lhs, BitFlag rhs) => lhs > rhs.flags_;
public static bool operator <=(BitFlag lhs, BitFlag rhs) => lhs.flags_ <= rhs.flags_;
public static bool operator <=(BitFlag lhs, int rhs) => lhs.flags_ <= rhs;
public static bool operator <=(int lhs, BitFlag rhs) => lhs <= rhs.flags_;
public static bool operator >=(BitFlag lhs, BitFlag rhs) => lhs.flags_ >= rhs.flags_;
public static bool operator >=(BitFlag lhs, int rhs) => lhs.flags_ >= rhs;
public static bool operator >=(int lhs, BitFlag rhs) => lhs >= rhs.flags_;
public static implicit operator BitFlag(int t) => new BitFlag(t);
public static implicit operator int(BitFlag t) => t.flags_;
//public int PopCount => (int)Popcnt.PopCount((uint)flags_);
public override string ToString() => $"{Convert.ToString(flags_, 2).PadLeft(32, '0')} ({flags_})";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ForEachSubBits(Action action)
{
for (BitFlag sub = flags_; sub >= 0; --sub) {
sub &= flags_;
action(sub);
}
}
}
public class HashMap : Dictionary
{
private readonly Func initialzier_;
public HashMap(Func initialzier)
: base()
{
initialzier_ = initialzier;
}
public HashMap(Func initialzier, int capacity)
: base(capacity)
{
initialzier_ = initialzier;
}
new public TValue this[TKey key]
{
get
{
if (TryGetValue(key, out TValue value)) {
return value;
} else {
var init = initialzier_(key);
base[key] = init;
return init;
}
}
set { base[key] = value; }
}
public HashMap Merge(
HashMap src,
Func mergeValues)
{
foreach (var key in src.Keys) {
this[key] = mergeValues(this[key], src[key]);
}
return this;
}
}
public struct ModInt
{
//public const long P = 1000000007;
public const long P = 998244353;
public const long ROOT = 3;
// (924844033, 5)
// (998244353, 3)
// (1012924417, 5)
// (167772161, 3)
// (469762049, 3)
// (1224736769, 3)
private long value_;
public ModInt(long value)
=> value_ = value;
public ModInt(long value, bool mods)
{
if (mods) {
value %= P;
if (value < 0) {
value += P;
}
}
value_ = value;
}
public static ModInt operator +(ModInt lhs, ModInt rhs)
{
lhs.value_ = (lhs.value_ + rhs.value_) % P;
return lhs;
}
public static ModInt operator +(long lhs, ModInt rhs)
{
rhs.value_ = (lhs + rhs.value_) % P;
return rhs;
}
public static ModInt operator +(ModInt lhs, long rhs)
{
lhs.value_ = (lhs.value_ + rhs) % P;
return lhs;
}
public static ModInt operator -(ModInt lhs, ModInt rhs)
{
lhs.value_ = (P + lhs.value_ - rhs.value_) % P;
return lhs;
}
public static ModInt operator -(long lhs, ModInt rhs)
{
rhs.value_ = (P + lhs - rhs.value_) % P;
return rhs;
}
public static ModInt operator -(ModInt lhs, long rhs)
{
lhs.value_ = (P + lhs.value_ - rhs) % P;
return lhs;
}
public static ModInt operator *(ModInt lhs, ModInt rhs)
{
lhs.value_ = lhs.value_ * rhs.value_ % P;
return lhs;
}
public static ModInt operator *(long lhs, ModInt rhs)
{
rhs.value_ = lhs * rhs.value_ % P;
return rhs;
}
public static ModInt operator *(ModInt lhs, long rhs)
{
lhs.value_ = lhs.value_ * rhs % P;
return lhs;
}
public static ModInt operator /(ModInt lhs, ModInt rhs)
{
long exp = P - 2;
while (exp > 0) {
if (exp % 2 > 0) {
lhs *= rhs;
}
rhs *= rhs;
exp /= 2;
}
return lhs;
}
public static implicit operator ModInt(long n) => new ModInt(n, true);
public static ModInt Inverse(ModInt value) => Pow(value, P - 2);
public static ModInt Pow(ModInt value, long k) => Pow(value.value_, k);
public static ModInt Pow(long value, long k)
{
long ret = 1;
for (k %= P - 1; k > 0; k >>= 1, value = value * value % P) {
if ((k & 1) == 1) {
ret = ret * value % P;
}
}
return new ModInt(ret);
}
public static Span NumberTheoreticTransform(
Span values, bool inverses = false)
{
var mods = new ModInt[values.Length];
for (int i = 0; i < mods.Length; i++) {
mods[i] = new ModInt(values[i]);
}
return NumberTheoreticTransform(mods, inverses);
}
public static Span NumberTheoreticTransform(
Span values, bool inverses = false)
{
var mods = new ModInt[values.Length];
for (int i = 0; i < mods.Length; i++) {
mods[i] = new ModInt(values[i]);
}
return NumberTheoreticTransform(mods, inverses);
}
public static Span NumberTheoreticTransform(
Span a, bool inverses = false)
{
int n = a.Length;
if (n == 1) {
return a;
}
var b = new ModInt[n].AsSpan();
int r = inverses
? (int)(P - 1 - (P - 1) / n)
: (int)((P - 1) / n);
ModInt s = Pow(ROOT, r);
var kp = new ModInt[n / 2 + 1];
kp.AsSpan().Fill(1);
for (int i = 0; i < n / 2; ++i) {
kp[i + 1] = kp[i] * s;
}
int l = n / 2;
for (int i = 1; i < n; i <<= 1, l >>= 1) {
r = 0;
for (int j = 0; j < l; ++j, r += i) {
s = kp[i * j];
for (int k = 0; k < i; ++k) {
var p = a[k + r];
var q = a[k + r + n / 2];
b[k + 2 * r] = p + q;
b[k + 2 * r + i] = (p - q) * s;
}
}
var temp = a;
a = b;
b = temp;
}
if (inverses) {
s = Inverse(n);
for (int i = 0; i < n; i++) {
a[i] = a[i] * s;
}
}
return a;
}
public static ModInt[,] NumberTheoreticTransform2D(ModInt[,] a, bool inverses = false)
{
int h = a.GetLength(0);
int w = a.GetLength(1);
if (h == 1 && w == 1) {
return a;
}
var b = new ModInt[h, w];
{
int n = w;
int r = inverses
? (int)(P - 1 - (P - 1) / n)
: (int)((P - 1) / n);
ModInt s = Pow(ROOT, r);
var kp = new ModInt[n / 2 + 1];
kp.AsSpan().Fill(1);
for (int i = 0; i < n / 2; ++i) {
kp[i + 1] = kp[i] * s;
}
for (int y = 0; y < h; y++) {
int l = n / 2;
for (int i = 1; i < n; i <<= 1, l >>= 1) {
r = 0;
for (int j = 0; j < l; ++j, r += i) {
s = kp[i * j];
for (int k = 0; k < i; ++k) {
var p = a[y, k + r];
var q = a[y, k + r + n / 2];
b[y, k + 2 * r] = p + q;
b[y, k + 2 * r + i] = (p - q) * s;
}
}
var temp = a;
a = b;
b = temp;
}
if (inverses) {
s = Inverse(n);
for (int i = 0; i < n; i++) {
a[y, i] = a[y, i] * s;
}
}
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
b[h, w] = 0;
}
}
{
int n = h;
int r = inverses
? (int)(P - 1 - (P - 1) / n)
: (int)((P - 1) / n);
ModInt s = Pow(ROOT, r);
var kp = new ModInt[n / 2 + 1];
kp.AsSpan().Fill(1);
for (int i = 0; i < n / 2; ++i) {
kp[i + 1] = kp[i] * s;
}
for (int x = 0; x < w; x++) {
int l = n / 2;
for (int i = 1; i < n; i <<= 1, l >>= 1) {
r = 0;
for (int j = 0; j < l; ++j, r += i) {
s = kp[i * j];
for (int k = 0; k < i; ++k) {
var p = a[k + r, x];
var q = a[k + r + n / 2, x];
b[k + 2 * r, x] = p + q;
b[k + 2 * r + i, x] = (p - q) * s;
}
}
var temp = a;
a = b;
b = temp;
}
if (inverses) {
s = Inverse(n);
for (int i = 0; i < n; i++) {
a[i, x] = a[i, x] * s;
}
}
}
}
return a;
}
public static Span Convolve(ReadOnlySpan a, ReadOnlySpan b)
{
int resultLength = a.Length + b.Length - 1;
int nttLenght = 1;
while (nttLenght < resultLength) {
nttLenght <<= 1;
}
var aa = new ModInt[nttLenght];
a.CopyTo(aa);
var bb = new ModInt[nttLenght];
b.CopyTo(bb);
var fa = NumberTheoreticTransform(aa);
var fb = NumberTheoreticTransform(bb);
for (int i = 0; i < nttLenght; i++) {
fa[i] *= fb[i];
}
var convolved = NumberTheoreticTransform(fa, true);
return convolved.Slice(0, resultLength);
}
public long ToLong() => value_;
public override string ToString() => value_.ToString();
}
public static class Helper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMin(this ref T target, T value) where T : struct, IComparable
=> target = target.CompareTo(value) > 0 ? value : target;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMin(this ref T target, T value, Action onUpdated)
where T : struct, IComparable
{
if (target.CompareTo(value) > 0) {
target = value;
onUpdated(value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMax(this ref T target, T value) where T : struct, IComparable
=> target = target.CompareTo(value) < 0 ? value : target;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMax(this ref T target, T value, Action onUpdated)
where T : struct, IComparable
{
if (target.CompareTo(value) < 0) {
target = value;
onUpdated(value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Array1(int n, T initialValue) where T : struct
=> new T[n].Fill(initialValue);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Array1(int n, Func initializer)
=> Enumerable.Range(0, n).Select(x => initializer(x)).ToArray();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Fill(this T[] array, T value)
where T : struct
{
array.AsSpan().Fill(value);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,] Array2(int n, int m, T initialValule) where T : struct
=> new T[n, m].Fill(initialValule);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,] Array2(int n, int m, Func initializer)
{
var array = new T[n, m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
array[i, j] = initializer(i, j);
}
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,] Fill(this T[,] array, T initialValue)
where T : struct
{
MemoryMarshal.CreateSpan(ref array[0, 0], array.Length).Fill(initialValue);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span AsSpan(this T[,] array, int i)
=> MemoryMarshal.CreateSpan(ref array[i, 0], array.GetLength(1));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,,] Array3(int n1, int n2, int n3, T initialValue)
where T : struct
=> new T[n1, n2, n3].Fill(initialValue);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,,] Fill(this T[,,] array, T initialValue)
where T : struct
{
MemoryMarshal.CreateSpan(ref array[0, 0, 0], array.Length).Fill(initialValue);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span AsSpan(this T[,,] array, int i, int j)
=> MemoryMarshal.CreateSpan(ref array[i, j, 0], array.GetLength(2));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,,,] Array4(int n1, int n2, int n3, int n4, T initialValue)
where T : struct
=> new T[n1, n2, n3, n4].Fill(initialValue);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,,,] Fill(this T[,,,] array, T initialValue)
where T : struct
{
MemoryMarshal.CreateSpan(ref array[0, 0, 0, 0], array.Length).Fill(initialValue);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span AsSpan(this T[,,,] array, int i, int j, int k)
=> MemoryMarshal.CreateSpan(ref array[i, j, k, 0], array.GetLength(3));
private static readonly int[] delta4_ = { 1, 0, -1, 0, 1 };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DoIn4(int i, int j, int imax, int jmax, Action action)
{
for (int dn = 0; dn < 4; ++dn) {
int d4i = i + delta4_[dn];
int d4j = j + delta4_[dn + 1];
if ((uint)d4i < (uint)imax && (uint)d4j < (uint)jmax) {
action(d4i, d4j);
}
}
}
private static readonly int[] delta8_ = { 1, 0, -1, 0, 1, 1, -1, -1, 1 };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DoIn8(int i, int j, int imax, int jmax, Action action)
{
for (int dn = 0; dn < 8; ++dn) {
int d8i = i + delta8_[dn];
int d8j = j + delta8_[dn + 1];
if ((uint)d8i < (uint)imax && (uint)d8j < (uint)jmax) {
action(d8i, d8j);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ForEachSubBits(int bit, Action action)
{
for (int sub = bit; sub >= 0; --sub) {
sub &= bit;
action(sub);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Reverse(string src)
{
var chars = src.ToCharArray();
for (int i = 0, j = chars.Length - 1; i < j; ++i, --j) {
var tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
}
return new string(chars);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Join(this IEnumerable values, string separator = "")
=> string.Join(separator, values);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string JoinNL