using System; using System.Buffers; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Intrinsics.X86; using System.Text; using YukicoderContest350.Problems; using ModInt = YukicoderContest350.Problems.ProblemE.StaticModInt; namespace YukicoderContest350.Problems { public class ProblemE : ProblemBase { public ProblemE() : base(false) { } [MethodImpl(MethodImplOptions.AggressiveOptimization)] protected override void SolveEach(IOManager io) { var n = io.ReadInt(); var k = io.ReadInt(); var a = io.ReadIntArray(n); var s = io.ReadString(); var dp = new ModInt[k + 1, n + 1]; for (int i = 0; i < n; i++) { dp[0, i] = ModInt.One; } var ascending = Enumerable.Range(0, n).OrderBy(i => a[i]).ThenByDescending(i => i).ToArray(); var descending = Enumerable.Range(0, n).OrderByDescending(i => a[i]).ThenByDescending(i => i).ToArray(); for (int i = 0; i < k; i++) { var order = s[i] == '<' ? ascending : descending; var bit = new BinaryIndexedTree(n); foreach (var j in order) { dp[i + 1, j] += bit.Sum(0, j); bit[j] += dp[i, j]; } } var result = ModInt.Zero; for (int i = 0; i <= n; i++) { result += dp[k, i]; } io.WriteLine(result); } public class BinaryIndexedTree { ModInt[] _data; public int Length { get; } public BinaryIndexedTree(int length) { _data = new ModInt[length + 1]; // 内部的には1-indexedにする Length = length; } public BinaryIndexedTree(IEnumerable data, int length) : this(length) { var count = 0; foreach (var n in data) { AddAt(count++, n); } } public BinaryIndexedTree(ICollection collection) : this(collection, collection.Count) { } public ModInt this[int index] { get => Sum(index, index + 1); set => AddAt(index, value - this[index]); } /// /// BITのindex番目の要素にnを加算します。 /// /// 加算するインデックス(0-indexed) /// 加算する数 public void AddAt(Index index, ModInt value) { var i = index.GetOffset(Length); unchecked { if ((uint)i >= (uint)Length) { throw new ArgumentOutOfRangeException(nameof(index)); } } i++; // 1-indexedにする while (i <= Length) { _data[i] += value; i += i & -i; // LSBの加算 } } /// /// [0, end)の部分和を返します。 /// /// 部分和を求める半開区間の終了インデックス /// 区間の部分和 public ModInt Sum(Index end) { var i = end.GetOffset(Length); // 0-indexedの半開区間=1-indexedの閉区間なので+1は不要 unchecked { if ((uint)i >= (uint)_data.Length) { throw new ArgumentOutOfRangeException(nameof(end)); } } ModInt sum = 0; while (i > 0) { sum += _data[i]; i -= i & -i; // LSBの減算 } return sum; } /// /// rangeの部分和を返します。 /// /// 部分和を求める半開区間 /// 区間の部分和 public ModInt Sum(Range range) => Sum(range.End) - Sum(range.Start); /// /// [start, end)の部分和を返します。 /// /// 部分和を求める半開区間の開始インデックス /// 部分和を求める半開区間の終了インデックス /// 区間の部分和 public ModInt Sum(int start, int end) => Sum(end) - Sum(start); } /// /// コンパイル時に決定する 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 : IEquatable> where T : struct, IStaticMod { private readonly uint _v; /// /// 格納されている値を返します。 /// public int Value => (int)_v; /// /// mod を返します。 /// public static int Mod => (int)default(T).Mod; public static StaticModInt Zero => new StaticModInt(); public static StaticModInt One => new StaticModInt(1u); /// /// に対して mod を取らずに StaticModInt<> 型のインスタンスを生成します。 /// /// /// 定数倍高速化のための関数です。 に 0 未満または mod 以上の値を入れた場合の挙動は未定義です。 /// 制約: 0≤||<mod /// [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; [MethodImpl(MethodImplOptions.AggressiveInlining)] private static uint Round(long v) { var x = v % default(T).Mod; if (x < 0) { x += default(T).Mod; } return (uint)x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static StaticModInt operator ++(StaticModInt value) { var v = value._v + 1; if (v == default(T).Mod) { v = 0; } return new StaticModInt(v); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static StaticModInt operator --(StaticModInt value) { var v = value._v; if (v == 0) { v = default(T).Mod; } return new StaticModInt(v - 1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] 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.Inverse(); 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 = Raw(1); while (n > 0) { if ((n & 1) > 0) { r *= x; } x *= x; n >>= 1; } return r; } /// /// 自身を x として、 xy≡1 なる y を返します。 /// /// /// 制約: gcd(x, mod) = 1 /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public StaticModInt Inverse() { 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 && Equals((StaticModInt)obj); public bool Equals(StaticModInt other) => Value == other.Value; public override int GetHashCode() => _v.GetHashCode(); } public static class InternalMath { /// /// 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; } } public static long SafeMod(long x, long m) { x %= m; if (x < 0) x += m; return x; } } } } namespace YukicoderContest350 { internal class Program { static void Main(string[] args) { IProblem question = new ProblemE(); using var io = new IOManager(Console.OpenStandardInput(), Console.OpenStandardOutput()); question.Solve(io); } } } #region Base Class namespace YukicoderContest350.Problems { public interface IProblem { string Solve(string input); void Solve(IOManager io); } public abstract class ProblemBase : IProblem { protected bool HasMultiTestCases { get; } protected ProblemBase(bool hasMultiTestCases) => HasMultiTestCases = hasMultiTestCases; public string Solve(string input) { var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(input)); var outputStream = new MemoryStream(); using var manager = new IOManager(inputStream, outputStream); Solve(manager); manager.Flush(); outputStream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(outputStream); return reader.ReadToEnd(); } public void Solve(IOManager io) { var tests = HasMultiTestCases ? io.ReadInt() : 1; for (var t = 0; t < tests; t++) { SolveEach(io); } } protected abstract void SolveEach(IOManager io); } } #endregion #region Utils namespace YukicoderContest350 { public class IOManager : IDisposable { private readonly BinaryReader _reader; private readonly StreamWriter _writer; private bool _disposedValue; private byte[] _buffer = new byte[1024]; private int _length; private int _cursor; private bool _eof; const char ValidFirstChar = '!'; const char ValidLastChar = '~'; public IOManager(Stream input, Stream output) { _reader = new BinaryReader(input); _writer = new StreamWriter(output) { AutoFlush = false }; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private char ReadAscii() { if (_cursor == _length) { _cursor = 0; _length = _reader.Read(_buffer); if (_length == 0) { if (!_eof) { _eof = true; return char.MinValue; } else { ThrowEndOfStreamException(); } } } return (char)_buffer[_cursor++]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public char ReadChar() { char c; while (!IsValidChar(c = ReadAscii())) { } return c; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadString() { var builder = new StringBuilder(); char c; while (!IsValidChar(c = ReadAscii())) { } do { builder.Append(c); } while (IsValidChar(c = ReadAscii())); return builder.ToString(); } public int ReadInt() => (int)ReadLong(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long ReadLong() { long result = 0; bool isPositive = true; char c; while (!IsNumericChar(c = ReadAscii())) { } if (c == '-') { isPositive = false; c = ReadAscii(); } do { result *= 10; result += c - '0'; } while (IsNumericChar(c = ReadAscii())); return isPositive ? result : -result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private Span ReadChunk(Span span) { var i = 0; char c; while (!IsValidChar(c = ReadAscii())) { } do { span[i++] = c; } while (IsValidChar(c = ReadAscii())); return span.Slice(0, i); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public double ReadDouble() => double.Parse(ReadChunk(stackalloc char[32])); [MethodImpl(MethodImplOptions.AggressiveInlining)] public decimal ReadDecimal() => decimal.Parse(ReadChunk(stackalloc char[32])); public int[] ReadIntArray(int n) { var a = new int[n]; for (int i = 0; i < a.Length; i++) { a[i] = ReadInt(); } return a; } public long[] ReadLongArray(int n) { var a = new long[n]; for (int i = 0; i < a.Length; i++) { a[i] = ReadLong(); } return a; } public double[] ReadDoubleArray(int n) { var a = new double[n]; for (int i = 0; i < a.Length; i++) { a[i] = ReadDouble(); } return a; } public decimal[] ReadDecimalArray(int n) { var a = new decimal[n]; for (int i = 0; i < a.Length; i++) { a[i] = ReadDecimal(); } return a; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Write(T value) => _writer.Write(value.ToString()); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteLine(T value) => _writer.WriteLine(value.ToString()); public void WriteLine(IEnumerable values, char separator) { var e = values.GetEnumerator(); if (e.MoveNext()) { _writer.Write(e.Current.ToString()); while (e.MoveNext()) { _writer.Write(separator); _writer.Write(e.Current.ToString()); } } _writer.WriteLine(); } public void WriteLine(T[] values, char separator) => WriteLine((ReadOnlySpan)values, separator); public void WriteLine(Span values, char separator) => WriteLine((ReadOnlySpan)values, separator); public void WriteLine(ReadOnlySpan values, char separator) { for (int i = 0; i < values.Length - 1; i++) { _writer.Write(values[i]); _writer.Write(separator); } if (values.Length > 0) { _writer.Write(values[^1]); } _writer.WriteLine(); } public void Flush() => _writer.Flush(); [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsValidChar(char c) => ValidFirstChar <= c && c <= ValidLastChar; [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsNumericChar(char c) => ('0' <= c && c <= '9') || c == '-'; private void ThrowEndOfStreamException() => throw new EndOfStreamException(); protected virtual void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { _reader.Dispose(); _writer.Flush(); _writer.Dispose(); } _disposedValue = true; } } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } } public static class UtilExtensions { public static bool ChangeMax(ref this T value, T other) where T : struct, IComparable { if (value.CompareTo(other) < 0) { value = other; return true; } return false; } public static bool ChangeMin(ref this T value, T other) where T : struct, IComparable { if (value.CompareTo(other) > 0) { value = other; return true; } return false; } public static void SwapIfLargerThan(ref this T a, ref T b) where T : struct, IComparable { if (a.CompareTo(b) > 0) { (a, b) = (b, a); } } public static void SwapIfSmallerThan(ref this T a, ref T b) where T : struct, IComparable { if (a.CompareTo(b) < 0) { (a, b) = (b, a); } } public static void Sort(this T[] array) where T : IComparable => Array.Sort(array); public static void Sort(this T[] array, Comparison comparison) => Array.Sort(array, comparison); } public static class CollectionExtensions { private class ArrayWrapper { #pragma warning disable CS0649 public T[] Array; #pragma warning restore CS0649 } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span AsSpan(this List list) { return Unsafe.As>(list).Array.AsSpan(0, list.Count); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span GetRowSpan(this T[,] array, int i) { var width = array.GetLength(1); return MemoryMarshal.CreateSpan(ref array[i, 0], width); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span GetRowSpan(this T[,,] array, int i, int j) { var width = array.GetLength(2); return MemoryMarshal.CreateSpan(ref array[i, j, 0], width); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span GetRowSpan(this T[,,,] array, int i, int j, int k) { var width = array.GetLength(3); return MemoryMarshal.CreateSpan(ref array[i, j, k, 0], width); } public static void Fill(this T[] array, T value) => array.AsSpan().Fill(value); public static void Fill(this T[,] array, T value) => MemoryMarshal.CreateSpan(ref array[0, 0], array.Length).Fill(value); public static void Fill(this T[,,] array, T value) => MemoryMarshal.CreateSpan(ref array[0, 0, 0], array.Length).Fill(value); public static void Fill(this T[,,,] array, T value) => MemoryMarshal.CreateSpan(ref array[0, 0, 0, 0], array.Length).Fill(value); } public static class SearchExtensions { private struct LowerBoundComparer : IComparer where T : IComparable { public int Compare(T x, T y) => 0 <= x.CompareTo(y) ? 1 : -1; } private struct UpperBoundComparer : IComparer where T : IComparable { public int Compare(T x, T y) => 0 < x.CompareTo(y) ? 1 : -1; } // https://trsing.hatenablog.com/entry/2019/08/27/211038 public static int GetGreaterEqualIndex(this ReadOnlySpan span, T inclusiveMin) where T : IComparable => ~span.BinarySearch(inclusiveMin, new UpperBoundComparer()); public static int GetGreaterThanIndex(this ReadOnlySpan span, T exclusiveMin) where T : IComparable => ~span.BinarySearch(exclusiveMin, new LowerBoundComparer()); public static int GetLessEqualIndex(this ReadOnlySpan span, T inclusiveMax) where T : IComparable => ~span.BinarySearch(inclusiveMax, new LowerBoundComparer()) - 1; public static int GetLessThanIndex(this ReadOnlySpan span, T exclusiveMax) where T : IComparable => ~span.BinarySearch(exclusiveMax, new UpperBoundComparer()) - 1; public static int GetGreaterEqualIndex(this Span span, T inclusiveMin) where T : IComparable => ((ReadOnlySpan)span).GetGreaterEqualIndex(inclusiveMin); public static int GetGreaterThanIndex(this Span span, T exclusiveMin) where T : IComparable => ((ReadOnlySpan)span).GetGreaterThanIndex(exclusiveMin); public static int GetLessEqualIndex(this Span span, T inclusiveMax) where T : IComparable => ((ReadOnlySpan)span).GetLessEqualIndex(inclusiveMax); public static int GetLessThanIndex(this Span span, T exclusiveMax) where T : IComparable => ((ReadOnlySpan)span).GetLessThanIndex(exclusiveMax); public static int BoundaryBinarySearch(Predicate predicate, int ok, int ng) { while (Math.Abs(ok - ng) > 1) { var mid = (ok + ng) / 2; if (predicate(mid)) { ok = mid; } else { ng = mid; } } return ok; } public static long BoundaryBinarySearch(Predicate predicate, long ok, long ng) { while (Math.Abs(ok - ng) > 1) { var mid = (ok + ng) / 2; if (predicate(mid)) { ok = mid; } else { ng = mid; } } return ok; } public static BigInteger BoundaryBinarySearch(Predicate predicate, BigInteger ok, BigInteger ng) { while (BigInteger.Abs(ok - ng) > 1) { var mid = (ok + ng) / 2; if (predicate(mid)) { ok = mid; } else { ng = mid; } } return ok; } public static double BoundaryBinarySearch(Predicate predicate, double ok, double ng, double eps = 1e-9, int loopLimit = 1000) { var count = 0; while (Math.Abs(ok - ng) > eps && count++ < loopLimit) { var mid = (ok + ng) * 0.5; if (predicate(mid)) { ok = mid; } else { ng = mid; } } return (ok + ng) * 0.5; } public static double Bisection(Func f, double a, double b, double eps = 1e-9, int loopLimit = 100) { double mid = (a + b) / 2; var fa = f(a); if (fa * f(b) >= 0) { throw new ArgumentException("f(a)とf(b)は異符号である必要があります。"); } for (int i = 0; i < loopLimit; i++) { var fmid = f(mid); var sign = fa * fmid; if (sign < 0) { b = mid; } else if (sign > 0) { a = mid; fa = fmid; } else { return mid; } mid = (a + b) / 2; if (Math.Abs(b - a) < eps) { break; } } return mid; } } } #endregion