Problem:
Score : 600 points

Problem Statement

Given are a sequence of N integers A_1, A_2, \ldots, A_N and a positive integer S.

For a pair of integers (L, R) such that 1\leq L \leq R \leq N, let us define f(L, R) as follows:

f(L, R) is the number of sequences of integers (x_1, x_2, \ldots , x_k) such that L \leq x_1 < x_2 < \cdots < x_k \leq R and A_{x_1}+A_{x_2}+\cdots +A_{x_k} = S.

Find the sum of f(L, R) over all pairs of integers (L, R) such that 1\leq L \leq R\leq N. Since this sum can be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq S \leq 3000

1 \leq A_i \leq 3000

Input

Input is given from Standard Input in the following format:

N S
A_1 A_2 ... A_N

Output

Print the sum of f(L, R), modulo 998244353.

Sample Input 1

3 4
2 2 4

Sample Output 1

5

The value of f(L, R) for each pair is as follows, for a total of 5.

f(1,1) = 0

f(1,2) = 1 (for the sequence (1, 2))

f(1,3) = 2 (for (1, 2) and (3))

f(2,2) = 0

f(2,3) = 1 (for (3))

f(3,3) = 1 (for (3))

Sample Input 2

5 8
9 9 9 9 9

Sample Output 2

0

Sample Input 3

10 10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

152

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using Yorukatsu035.Questions;
using Yorukatsu035.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;

namespace Yorukatsu035.Questions
{
    /// <summary>
    /// ABC159 F
    /// </summary>
    public class QuestionF : AtCoderQuestionBase
    {
        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var ns = inputStream.ReadIntArray();
            var s = ns[1];
            var a = inputStream.ReadIntArray();

            const int mod = 998244353;
            var counts = new Modular[a.Length + 1, s + 1, 3];   // 3引数目は状態（0:L未決定, 1:R未決定, 2:両方決定）
            Clear(counts, mod);
            counts[0, 0, 0] = new Modular(1);

            for (int item = 0; item < a.Length; item++)
            {
                for (int sum = 0; sum <= s; sum++)
                {
                    // 0
                    counts[item + 1, sum, 0] += counts[item, sum, 0];   // 0 -> 0
                    if (sum + a[item] <= s)
                    {
                        counts[item + 1, sum + a[item], 1] += counts[item, sum, 0] * new Modular(item + 1);   // 0 -> 1
                        counts[item + 1, sum + a[item], 2] += counts[item, sum, 0] * new Modular(item + 1) * new Modular(a.Length - item);   // 0 -> 1 -> 2
                    }

                    // 1
                    counts[item + 1, sum, 1] += counts[item, sum, 1];   // 1 -> 1
                    if (sum + a[item] <= s)
                    {
                        counts[item + 1, sum + a[item], 1] += counts[item, sum, 1]; // 1 -> 1
                        counts[item + 1, sum + a[item], 2] += counts[item, sum, 1] * new Modular(a.Length - item); // 1 -> 2
                    }

                    // 2
                    counts[item + 1, sum, 2] += counts[item, sum, 2];   // 2 -> 2
                }
            }

            yield return counts[a.Length, s, 2].Value;
        }

        private static void Clear(Modular[,,] counts, int mod)
        {
            for (int i = 0; i < counts.GetLength(0); i++)
            {
                for (int j = 0; j < counts.GetLength(1); j++)
                {
                    for (int k = 0; k < counts.GetLength(2); k++)
                    {
                        counts[i, j, k] = new Modular(0);
                    }
                }
            }
        }

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Auto)]
        public struct Modular : IEquatable<Modular>, IComparable<Modular>
        {
            private const int Mod = 998244353;
            private readonly long _value;    // 0 <= value < Mod の値をとる
            public int Value => (int)_value;

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public Modular(long value)
            {
                if (value >= 0 && value < Mod)
                {
                    _value = value;
                }
                else
                {
                    _value = value % Mod;
                    if (Value < 0)
                    {
                        _value += Mod;
                    }
                }
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public static Modular operator +(Modular a, Modular b)
            {
                var result = a._value + b._value;
                if (result > Mod)
                {
                    result -= Mod;    // 剰余演算を避ける
                }
                return new Modular(result);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public static Modular operator -(Modular a, Modular b)
            {
                var result = a._value - b._value;
                if (result < 0)
                {
                    result += Mod;    // 剰余演算を避ける
                }
                return new Modular(result);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public static Modular operator *(Modular a, Modular b)
            {
                return new Modular(a._value * b._value);
            }

            // 需要は不明だけど一応
            public static bool operator ==(Modular left, Modular right) => left.Equals(right);
            public static bool operator !=(Modular left, Modular right) => !(left == right);
            public static bool operator <(Modular left, Modular right) => left.CompareTo(right) < 0;
            public static bool operator <=(Modular left, Modular right) => left.CompareTo(right) <= 0;
            public static bool operator >(Modular left, Modular right) => left.CompareTo(right) > 0;
            public static bool operator >=(Modular left, Modular right) => left.CompareTo(right) >= 0;

            public static explicit operator int(Modular a) => a.Value;
            public static explicit operator long(Modular a) => a._value;


            public override string ToString() => $"{_value} (mod {Mod})";

            public bool Equals(Modular other) => _value == other._value;

            public int CompareTo(Modular other) => _value.CompareTo(other._value);

            public override int GetHashCode() => Value ^ Mod;
        }

    }
}


namespace Yorukatsu035
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionF();    // 問題に合わせて書き換え
            var answers = question.Solve(Console.In);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
        }
    }
}

#region Base Classes

namespace Yorukatsu035.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }

}

#endregion

#region Extensions

namespace Yorukatsu035.Extensions
{
    internal static class TextReaderExtensions
    {
        internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        internal static string ReadString(this TextReader reader) => reader.ReadLine();

        internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);
    }
}

#endregion