Problem:
Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

0 l r b c: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c.

1 l r: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353.

Constraints

1 \leq N, Q \leq 500000

0 \leq a_i, c < 998244353

1 \leq b < 998244353

0 \leq l < r \leq N

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
a_0 a_1 ... a_{N - 1}
\textrm{Query}_0
\textrm{Query}_1
:
\textrm{Query}_{Q - 1}

Output

For each query of the latter type, print the answer.

Sample Input 1

5 7
1 2 3 4 5
1 0 5
0 2 4 100 101
1 0 3
0 1 3 102 103
1 2 5
0 2 5 104 105
1 0 5

Sample Output 1

15
404
41511
4317767

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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.Runtime.Intrinsics.X86;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace AtCoder
{
	public class Program
	{
		static void Main()
		{
			using var cin = new Scanner();
			var (n, q) = cin.Int2();
			var a = cin.ArrayInt(n).Select(x => (new ModInt(x), 1)).ToArray();

			var seg = new LazySegtree<(ModInt v, int n), (ModInt a, ModInt b), Monoid>(a);
			for (int i = 0; i < q; i++) {
				int t = cin.Int();
				if (t == 0) {
					var (l, r, b, c) = cin.Int4();
					seg.Apply(l, r, (b, c));
				} else {
					var (l, r) = cin.Int2();
					Console.WriteLine(seg.Prod(l, r).v);
				}
			}
		}

		public struct Monoid : IMonoidFuncOperator<(ModInt v, int n), (ModInt a, ModInt b)>
		{
			public (ModInt v, int n) Identity => (0, 0);

			public (ModInt a, ModInt b) FIdentity => (1, 0);

			public (ModInt a, ModInt b) Composition((ModInt a, ModInt b) f, (ModInt a, ModInt b) g)
			{
				return (g.a * f.a, g.b * f.a + f.b);
			}

			public (ModInt v, int n) Mapping((ModInt a, ModInt b) f, (ModInt v, int n) x)
			{
				return (x.v * f.a + f.b * x.n, x.n);
			}

			public (ModInt v, int n) Operate((ModInt v, int n) x, (ModInt v, int n) y)
			{
				return (x.v + y.v, x.n + y.n);
			}
		}
	}

	/// <summary>
	/// モノイドを定義するインターフェイスです。
	/// </summary>
	/// <typeparam name="T">操作を行う型。</typeparam>
	/// <typeparam name="F">写像の型。</typeparam>
	public interface IMonoidFuncOperator<T, F>
	{
		/// <summary>
		/// Operate(x, <paramref name="Identity"/>) = x を満たす単位元。
		/// </summary>
		T Identity { get; }
		/// <summary>
		/// Mapping(<paramref name="FIdentity"/>, x) = x を満たす恒等写像。
		/// </summary>
		F FIdentity { get; }
		/// <summary>
		/// 結合律 Operate(Operate(a, b), c) = Operate(a, Operate(b, c)) を満たす写像。
		/// </summary>
		T Operate(T x, T y);
		/// <summary>
		/// 写像　<paramref name="f"/> を <paramref name="x"/> に作用させる関数。
		/// </summary>
		T Mapping(F f, T x);
		/// <summary>
		/// 写像　<paramref name="f"/>, <paramref name="g"/> を 合成した写像 <paramref name="f"/>∘<paramref name="g"/>。
		/// </summary>
		F Composition(F f, F g);
	}

	public static partial class InternalMath
	{
		/// <summary>
		/// <paramref name="n"/> ≤ 2**x を満たす最小のx
		/// </summary>
		/// <remarks>
		/// <para>制約: 0≤<paramref name="n"/></para>
		/// </remarks>
		public static int CeilPow2(int n)
		{
			var un = (uint)n;
			if (un <= 1) return 0;
			return BitOperations.Log2(un - 1) + 1;
		}
	}

	/// <summary>
	/// 長さ N の配列に対し、
	/// <list type="bullet">
	/// <item>
	/// <description>区間の要素に一括で <typeparamref name="TOp"/> の要素 f を作用 ( x=f(x) )</description>
	/// </item>
	/// <item>
	/// <description>区間の要素の総積の取得</description>
	/// </item>
	/// </list>
	/// <para>を O(log N) で求めることが出来るデータ構造です。</para>
	/// </summary>
	public class LazySegtree<TValue, F, TOp> where TOp : struct, IMonoidFuncOperator<TValue, F>
	{
		private static readonly TOp op = default;

		/// <summary>
		/// 数列 a の長さ n を返します。
		/// </summary>
		public int Length { get; }

		private readonly int log;
		private readonly int size;
		private readonly TValue[] d;
		private readonly F[] lz;


		/// <summary>
		/// 長さ <paramref name="n"/> の数列 a　を持つ <see cref="LazySegtree{TValue, TOp}"/> クラスの新しいインスタンスを作ります。初期値は <see cref="TOp.Identity"/> です。
		/// </summary>
		/// <remarks>
		/// <para>制約: 0≤<paramref name="n"/>≤10^8</para>
		/// <para>計算量: O(<paramref name="n"/>)</para>
		/// </remarks>
		/// <param name="n">配列の長さ</param>
		public LazySegtree(int n)
		{
			AssertMonoid(op.Identity);
			AssertFIdentity(op.Identity);
			AssertF(op.FIdentity, op.Identity, op.Identity);
			Length = n;
			log = InternalMath.CeilPow2(n);
			size = 1 << log;
			d = new TValue[2 * size];
			lz = new F[size];
			Array.Fill(d, op.Identity);
			Array.Fill(lz, op.FIdentity);
		}

		/// <summary>
		/// 長さ n=<paramref name="v"/>.Length の数列 a　を持つ <see cref="LazySegtree{TValue, TOp}"/> クラスの新しいインスタンスを作ります。初期値は <paramref name="v"/> です。
		/// </summary>
		/// <remarks>
		/// <para>制約: 0≤<paramref name="n"/>≤10^8</para>
		/// <para>計算量: O(<paramref name="n"/>)</para>
		/// </remarks>
		/// <param name="n">配列の長さ</param>
		public LazySegtree(TValue[] v) : this(v.Length)
		{
			for (int i = 0; i < v.Length; i++) d[size + i] = v[i];
			for (int i = size - 1; i >= 1; i--) {
				Update(i);
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private void Update(int k) => d[k] = op.Operate(d[2 * k], d[2 * k + 1]);

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private void AllApply(int k, F f)
		{
			AssertF(f, op.Identity, op.Identity);
			AssertMonoid(d[k]);
			AssertFIdentity(d[k]);
			AssertF(f, d[k], d[k]);
			d[k] = op.Mapping(f, d[k]);
			if (k < size) lz[k] = op.Composition(f, lz[k]);
		}
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private void Push(int k)
		{
			AllApply(2 * k, lz[k]);
			AllApply(2 * k + 1, lz[k]);
			lz[k] = op.FIdentity;
		}

		/// <summary>
		/// a[<paramref name="p"/>] を返します。
		/// </summary>
		/// <remarks>
		/// <para>制約: 0≤<paramref name="p"/>&lt;n</para>
		/// <para>計算量: O(log n)</para>
		/// </remarks>
		/// <returns></returns>
		public TValue this[int p]
		{
			[MethodImpl(MethodImplOptions.AggressiveInlining)]
			set
			{
				Debug.Assert((uint)p < Length);
				p += size;
				for (int i = log; i >= 1; i--) Push(p >> i);
				d[p] = value;
				for (int i = 1; i <= log; i++) Update(p >> i);
			}
			[MethodImpl(MethodImplOptions.AggressiveInlining)]
			get
			{
				Debug.Assert((uint)p < Length);
				p += size;
				for (int i = log; i >= 1; i--) Push(p >> i);
				return d[p];
			}
		}

		/// <summary>
		/// <see cref="TOp.Operate"/>(a[<paramref name="l"/>], ..., a[<paramref name="r"/> - 1]) を返します。<paramref name="l"/> = <paramref name="r"/> のときは　<see cref="TOp.Identity"/> を返します。
		/// </summary>
		/// <remarks>
		/// <para>制約: 0≤<paramref name="l"/>≤<paramref name="r"/>≤n</para>
		/// <para>計算量: O(log n)</para>
		/// </remarks>
		/// <returns><see cref="TOp.Operate"/>(a[<paramref name="l"/>], ..., a[<paramref name="r"/> - 1])</returns>
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public TValue Prod(int l, int r)
		{
			Debug.Assert(0 <= l && l <= r && r <= Length);
			if (l == r) return op.Identity;

			l += size;
			r += size;

			for (int i = log; i >= 1; i--) {
				if (((l >> i) << i) != l) Push(l >> i);
				if (((r >> i) << i) != r) Push(r >> i);
			}

			TValue sml = op.Identity, smr = op.Identity;
			while (l < r) {
				if ((l & 1) != 0) sml = op.Operate(sml, d[l++]);
				if ((r & 1) != 0) smr = op.Operate(d[--r], smr);
				l >>= 1;
				r >>= 1;
			}

			return op.Operate(sml, smr);
		}

		/// <summary>
		/// <see cref="TOp.Operate"/>(a[0], ..., a[n - 1]) を返します。n = 0 のときは　<see cref="TOp.Identity"/> を返します。
		/// </summary>
		/// <remarks>
		/// <para>計算量: O(1)</para>
		/// </remarks>
		/// <returns><see cref="TOp.Operate"/>(a[0], ..., a[n - 1])</returns>
		public TValue AllProd => d[1];

		/// <summary>
		/// a[<paramref name="p"/>] = <see cref="TOp.Mapping"/>(<paramref name="f"/>, a[<paramref name="p"/>])
		/// </summary>
		/// <remarks>
		/// <para>制約: 0≤<paramref name="p"/>≤n</para>
		/// <para>計算量: O(log n)</para>
		/// </remarks>
		public void Apply(int p, F f)
		{
			Debug.Assert((uint)p < Length);
			p += size;
			for (int i = log; i >= 1; i--) Push(p >> i);
			d[p] = op.Mapping(f, d[p]);
			for (int i = 1; i <= log; i++) Update(p >> i);
		}

		/// <summary>
		/// i = <paramref name="l"/>..<paramref name="r"/> について a[i] = <see cref="TOp.Mapping"/>(<paramref name="f"/>, a[<paramref name="p"/>])
		/// </summary>
		/// <remarks>
		/// <para>制約: 0≤<paramref name="l"/>≤<paramref name="r"/>≤n</para>
		/// <para>計算量: O(log n)</para>
		/// </remarks>
		public void Apply(int l, int r, F f)
		{
			Debug.Assert(0 <= l && l <= r && r <= Length);
			if (l == r) return;

			l += size;
			r += size;

			for (int i = log; i >= 1; i--) {
				if (((l >> i) << i) != l) Push(l >> i);
				if (((r >> i) << i) != r) Push((r - 1) >> i);
			}

			{
				int l2 = l, r2 = r;
				while (l < r) {
					if ((l & 1) != 0) AllApply(l++, f);
					if ((r & 1) != 0) AllApply(--r, f);
					l >>= 1;
					r >>= 1;
				}
				l = l2;
				r = r2;
			}

			for (int i = 1; i <= log; i++) {
				if (((l >> i) << i) != l) Update(l >> i);
				if (((r >> i) << i) != r) Update((r - 1) >> i);
			}
		}



		/// <summary>
		/// 以下の条件を両方満たす r を(いずれか一つ)返します。
		/// <list type="bullet">
		/// <item>
		/// <description>r = <paramref name="l"/> もしくは <paramref name="g"/>(op(a[<paramref name="l"/>], a[<paramref name="l"/> + 1], ..., a[r - 1])) = true</description>
		/// </item>
		/// <item>
		/// <description>r = n もしくは <paramref name="g"/>(op(a[<paramref name="l"/>], a[<paramref name="l"/> + 1], ..., a[r])) = false</description>
		/// </item>
		/// </list>
		/// <para><paramref name="g"/> が単調だとすれば、<paramref name="g"/>(op(a[<paramref name="l"/>], a[<paramref name="l"/> + 1], ..., a[r - 1])) = true となる最大の r、と解釈することが可能です。</para>
		/// </summary>
		/// <remarks>
		/// 制約
		/// <list type="bullet">
		/// <item>
		/// <description><paramref name="g"/> を同じ引数で呼んだ時、返り値は等しい(=副作用はない)。</description>
		/// </item>
		/// <item>
		/// <description><paramref name="g"/>(<see cref="TOp.Identity"/>) = true</description>
		/// </item>
		/// <item>
		/// <description>0≤<paramref name="l"/>≤n</description>
		/// </item>
		/// </list>
		/// <para>計算量: O(log n)</para>
		/// </remarks>
		public int MaxRight(int l, Predicate<TValue> g)
		{
			Debug.Assert((uint)l <= Length);
			Debug.Assert(g(op.Identity));
			if (l == Length) return Length;
			l += size;
			for (int i = log; i >= 1; i--) Push(l >> i);
			TValue sm = op.Identity;
			do {
				while (l % 2 == 0) l >>= 1;
				if (!g(op.Operate(sm, d[l]))) {
					while (l < size) {
						Push(l);
						l = (2 * l);
						if (g(op.Operate(sm, d[l]))) {
							sm = op.Operate(sm, d[l]);
							l++;
						}
					}
					return l - size;
				}
				sm = op.Operate(sm, d[l]);
				l++;
			} while ((l & -l) != l);

			return Length;
		}

		/// <summary>
		/// 以下の条件を両方満たす r を(いずれか一つ)返します。
		/// <list type="bullet">
		/// <item>
		/// <description>l = <paramref name="r"/> もしくは <paramref name="g"/>(op(a[l], a[l + 1], ..., a[<paramref name="r"/> - 1])) = true</description>
		/// </item>
		/// <item>
		/// <description>l = 0 もしくは <paramref name="g"/>(op(a[l - 1], a[l], ..., a[<paramref name="r"/> - 1])) = false</description>
		/// </item>
		/// </list>
		/// <para><paramref name="g"/> が単調だとすれば、<paramref name="g"/>(op(a[l], a[l + 1], ..., a[<paramref name="r"/> - 1])) = true となる最小の l、と解釈することが可能です。</para>
		/// </summary>
		/// <remarks>
		/// 制約
		/// <list type="bullet">
		/// <item>
		/// <description><paramref name="g"/> を同じ引数で呼んだ時、返り値は等しい(=副作用はない)。</description>
		/// </item>
		/// <item>
		/// <description><paramref name="g"/>(<see cref="TOp.Identity"/>) = true</description>
		/// </item>
		/// <item>
		/// <description>0≤<paramref name="r"/>≤n</description>
		/// </item>
		/// </list>
		/// <para>計算量: O(log n)</para>
		/// </remarks>
		public int MinLeft(int r, Predicate<TValue> g)
		{
			Debug.Assert((uint)r <= Length);
			Debug.Assert(g(op.Identity));
			if (r == 0) return 0;
			r += size;
			for (int i = log; i >= 1; i--) Push((r - 1) >> i);
			TValue sm = op.Identity;
			do {
				r--;
				while (r > 1 && (r % 2) != 0) r >>= 1;
				if (!g(op.Operate(d[r], sm))) {
					while (r < size) {
						Push(r);
						r = (2 * r + 1);
						if (g(op.Operate(d[r], sm))) {
							sm = op.Operate(d[r], sm);
							r--;
						}
					}
					return r + 1 - size;
				}
				sm = op.Operate(d[r], sm);
			} while ((r & -r) != r);
			return 0;
		}


		[DebuggerDisplay("Value = {" + nameof(value) + "}, Lazy = {" + nameof(lazy) + "}", Name = "{" + nameof(key) + ",nq}")]
		private struct DebugItem
		{
			public DebugItem(int l, int r, TValue value, F lazy)
			{
				if (r - l == 1)
					key = $"[{l}]";
				else
					key = $"[{l}-{r})";
				this.value = value;
				this.lazy = lazy;
			}
			[DebuggerBrowsable(DebuggerBrowsableState.Never)]
			private readonly string key;
			[DebuggerBrowsable(DebuggerBrowsableState.Never)]
			private readonly TValue value;
			[DebuggerBrowsable(DebuggerBrowsableState.Never)]
			private readonly F lazy;
		}
		private class DebugView
		{
			private readonly LazySegtree<TValue, F, TOp> segtree;
			public DebugView(LazySegtree<TValue, F, TOp> segtree)
			{
				this.segtree = segtree;
			}
			[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
			public DebugItem[] Items
			{
				get
				{
					var items = new List<DebugItem>(segtree.Length);
					for (int len = segtree.size; len > 0; len >>= 1) {
						int unit = segtree.size / len;
						for (int i = 0; i < len; i++) {
							int l = i * unit;
							int r = System.Math.Min(l + unit, segtree.Length);
							if (l < segtree.Length) {
								int dataIndex = i + len;
								if ((uint)dataIndex < segtree.lz.Length)
									items.Add(new DebugItem(l, r, segtree.d[dataIndex], segtree.lz[dataIndex]));
								else
									items.Add(new DebugItem(l, r, segtree.d[dataIndex], op.FIdentity));
							}
						}
					}
					return items.ToArray();
				}
			}
		}


		/// <summary>
		/// Debug ビルドにおいて、Monoid が正しいかチェックする。
		/// </summary>
		/// <param name="value"></param>
		[Conditional("DEBUG")]
		public static void AssertMonoid(TValue value)
		{
			Debug.Assert(op.Operate(value, op.Identity).Equals(value),
				$"{nameof(op.Operate)}({value}, {op.Identity}) != {value}");
			Debug.Assert(op.Operate(op.Identity, value).Equals(value),
				$"{nameof(op.Operate)}({op.Identity}, {value}) != {value}");
		}

		/// <summary>
		/// Debug ビルドにおいて、FIdentity が恒等写像かチェックする。
		/// </summary>
		/// <param name="value"></param>
		[Conditional("DEBUG")]
		public static void AssertFIdentity(TValue value)
		{
			Debug.Assert(op.Mapping(op.FIdentity, value).Equals(value),
				$"{nameof(op.Mapping)}({op.Identity}, {value}) != {value}");
		}

		/// <summary>
		/// Debug ビルドにおいて、F が分配法則を満たすかチェックする。
		/// </summary>
		/// <param name="value"></param>
		[Conditional("DEBUG")]
		public static void AssertF(F f, TValue v1, TValue v2)
		{
			Debug.Assert(op.Mapping(op.FIdentity, op.Operate(v1, v2)).Equals(op.Operate(op.Mapping(op.FIdentity, v1), op.Mapping(op.FIdentity, v2))),
				$"{nameof(op.Mapping)}({nameof(op.Operate)}({v1}, {v2})) != {nameof(op.Operate)}({nameof(op.Mapping)}({op.Identity}, {v1}), {nameof(op.Mapping)}({op.Identity}, {v2}))");
		}
	}

	public class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
	{
		private readonly Func<TKey, TValue> initialzier_;
		public HashMap(Func<TKey, TValue> initialzier)
			: base()
		{
			initialzier_ = initialzier;
		}

		public HashMap(Func<TKey, TValue> initialzier, int capacity)
			: base(capacity)
		{
			initialzier_ = initialzier;
		}

		new public TValue this[TKey key]
		{
			get
			{
				if (ContainsKey(key) == false) {
					base[key] = initialzier_(key);
				}

				return base[key];
			}

			set { base[key] = value; }
		}
	}

	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<ModInt> NumberTheoreticTransform(
			Span<int> 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<ModInt> NumberTheoreticTransform(
			Span<long> 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<ModInt> NumberTheoreticTransform(
			Span<ModInt> 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 Span<ModInt> Convolve(ReadOnlySpan<ModInt> a, ReadOnlySpan<ModInt> 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<T>(ref T target, T value) where T : IComparable<T>
			=> target = target.CompareTo(value) > 0 ? value : target;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMin<T>(ref T target, T value, Action<T> onUpdated) where T : IComparable<T>
		{
			if (target.CompareTo(value) > 0) {
				target = value;
				onUpdated(value);
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMax<T>(ref T target, T value) where T : IComparable<T>
			=> target = target.CompareTo(value) < 0 ? value : target;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMax<T>(ref T target, T value, Action<T> onUpdated) where T : IComparable<T>
		{
			if (target.CompareTo(value) < 0) {
				target = value;
				onUpdated(value);
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[] Array<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToArray();
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static List<T> List<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToList();

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,] Array2<T>(int n, int m, T init)
			where T : struct
		{
			var array = new T[n, m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					array[i, j] = init;
				}
			}

			return array;
		}
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,] Array2<T>(int n, int m, Func<int, int, T> 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[,,] Array3<T>(int n1, int n2, int n3, T init)
			where T : struct
		{
			var array = new T[n1, n2, n3];
			for (int i1 = 0; i1 < n1; i1++) {
				for (int i2 = 0; i2 < n2; i2++) {
					for (int i3 = 0; i3 < n3; i3++) {
						array[i1, i2, i3] = init;
					}
				}
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,,,] Array4<T>(int n1, int n2, int n3, int n4, T init)
			where T : struct
		{
			var array = new T[n1, n2, n3, n4];
			for (int i1 = 0; i1 < n1; i1++) {
				for (int i2 = 0; i2 < n2; i2++) {
					for (int i3 = 0; i3 < n3; i3++) {
						for (int i4 = 0; i4 < n4; i4++) {
							array[i1, i2, i3, i4] = init;
						}
					}
				}
			}

			return array;
		}

		private static readonly int[] delta4_ = { 1, 0, -1, 0, 1 };
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void DoAt4(int i, int j, int imax, int jmax, Action<int, int> 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 DoAt8(int i, int j, int imax, int jmax, Action<int, int> 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 ForEachSubBit(int bit, Action<int> 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<T>(this IEnumerable<T> values, string separator = "")
			=> string.Join(separator, values);
	}

	public class Scanner : IDisposable
	{
		private const int BUFFER_SIZE = 1024;
		private const int ASCII_CHAR_BEGIN = 33;
		private const int ASCII_CHAR_END = 126;
		private readonly string filePath_;
		private readonly Stream stream_;
		private readonly byte[] buf_ = new byte[BUFFER_SIZE];
		private int length_ = 0;
		private int index_ = 0;
		private bool isEof_ = false;

		public Scanner(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				stream_ = Console.OpenStandardInput();
			} else {
				filePath_ = file;
				stream_ = new FileStream(file, FileMode.Open);
			}

			Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush = false
			});
		}

		public void Dispose()
		{
			Console.Out.Flush();
			stream_.Dispose();
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public char Char()
		{
			byte b;
			do {
				b = Read();
			} while (b < ASCII_CHAR_BEGIN || ASCII_CHAR_END < b);

			return (char)b;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public string Next()
		{
			var sb = new StringBuilder();
			for (var b = Char(); b >= ASCII_CHAR_BEGIN && b <= ASCII_CHAR_END; b = (char)Read()) {
				sb.Append(b);
			}

			return sb.ToString();
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int Int() => (int)Long();
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int Int(int offset) => Int() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int) Int2(int offset = 0)
			=> (Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int, int) Int3(int offset = 0)
			=> (Int(offset), Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int, int, int) Int4(int offset = 0)
			=> (Int(offset), Int(offset), Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int[] ArrayInt(int length, int offset = 0)
		{
			int[] Array = new int[length];
			for (int i = 0; i < length; i++) {
				Array[i] = Int(offset);
			}
			return Array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long Long()
		{
			long ret = 0;
			byte b;
			bool ng = false;
			do {
				b = Read();
			} while (b != '-' && (b < '0' || '9' < b));

			if (b == '-') {
				ng = true;
				b = Read();
			}

			for (; true; b = Read()) {
				if (b < '0' || '9' < b) {
					return ng ? -ret : ret;
				} else {
					ret = ret * 10 + b - '0';
				}
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long Long(long offset) => Long() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long) Long2(long offset = 0)
			=> (Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long, long) Long3(long offset = 0)
			=> (Long(offset), Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long, long, long) Long4(long offset = 0)
			=> (Long(offset), Long(offset), Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long[] ArrayLong(int length, long offset = 0)
		{
			long[] Array = new long[length];
			for (int i = 0; i < length; i++) {
				Array[i] = Long(offset);
			}
			return Array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double Double() => double.Parse(Next(), CultureInfo.InvariantCulture);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double Double(double offset) => Double() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double) Double2(double offset = 0)
			=> (Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double, double) Double3(double offset = 0)
			=> (Double(offset), Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double, double, double) Double4(double offset = 0)
			=> (Double(offset), Double(offset), Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double[] ArrayDouble(int length, double offset = 0)
		{
			double[] Array = new double[length];
			for (int i = 0; i < length; i++) {
				Array[i] = Double(offset);
			}
			return Array;
		}

		private byte Read()
		{
			if (isEof_) {
				throw new EndOfStreamException();
			}

			if (index_ >= length_) {
				index_ = 0;
				if ((length_ = stream_.Read(buf_, 0, BUFFER_SIZE)) <= 0) {
					isEof_ = true;
					return 0;
				}
			}

			return buf_[index_++];
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}