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 System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder20200322
{
    class Program
    {
        static void Main(string[] args)
        {
            // TheNumberOfEvenPairs();
            //  StringPalindrome();
            // MaxVolume();
            // BannedK();
            KnapsackforAllSegments();
        }
        
        public static void TheNumberOfEvenPairs()
        {
            String[] inputs = Console.ReadLine().Split(' ');
            int n = int.Parse(inputs[0]);
            int m = int.Parse(inputs[1]);

            int sum = nCm(n, 2) + nCm(m, 2);
            Console.WriteLine(sum);
        }

        public static int nCm(int n, int m)
        {
            if (m == 0) return 1;
            if (n == 0) return 0;
            return n * nCm(n - 1, m - 1) / m;
        }

        public static void MaxVolume()
        {
            double l = double.Parse(Console.ReadLine());
            double vol = Math.Pow(l, 3) / (double)27;
            Console.WriteLine(vol);
        }

        public static void BannedK()
        {
            int n = int.Parse(Console.ReadLine());
            String[] input_a = Console.ReadLine().Split(' ');
            int[] a = new int[n];
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i=0; i<n; i++)
            {
                a[i] = int.Parse(input_a[i]);
                if (dic.ContainsKey(a[i]))
                {
                    dic[a[i]] += 1;
                }
                else
                {
                    dic.Add(a[i], 1);
                }
            }
            Dictionary<int, int> sum_dic = new Dictionary<int, int>();
            Dictionary<int, int> reduced_sum_dic = new Dictionary<int, int>();
            int sum = 0;
            
            foreach(int key in dic.Keys)
            {
                int sum_key = nCm(dic[key], 2);
                sum_dic.Add(key,sum_key);
                reduced_sum_dic.Add(key, nCm(dic[key] - 1, 2));
                sum += sum_key;
            }

            // Result
            foreach(int i in a)
            {
                int res = sum - sum_dic[i] + reduced_sum_dic[i];
                Console.WriteLine(res);
            }
        }

        public static void StringPalindrome()
        {
            String s = Console.ReadLine();
            int n = s.Length;
            char[] cs = new char[n];
            char[] c_former = new char[(n - 1) / 2];
            char[] c_latter = new char[(n - 1) / 2];
            int i = 0;
            foreach(char c in s)
            {
                cs[i] = c;
                i++;
            }
            bool isKaibun = true;
            for(int j=0; j<(n-1)/2; j++)
            {
                isKaibun = isKaibun && (cs[j] == cs[n - 1 - j]);
                c_former[j] = cs[j];
                c_latter[(n - 1) / 2 - 1- j] = cs[n - 1 - j];
            }
            if(!isKaibun)
            {
                Console.WriteLine("No");
                return;
            }
            for(int j=0; j<c_former.Length; j++)
            {
                isKaibun = isKaibun && (c_former[j] == c_former[c_former.Length - 1 - j]) && (c_latter[j] == c_latter[c_latter.Length - 1 - j]);   
            }
            if(isKaibun)
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }


        }

        public static void KnapsackforAllSegments()
        {
            String[] input1 = Console.ReadLine().Split(' ');
            String[] input2 = Console.ReadLine().Split(' ');

            int n = int.Parse(input1[0]);
            int s = int.Parse(input1[1]);
            int[] a = new int[n];
            for(int i=0; i<n; i++)
            {
                a[i] = int.Parse(input2[i]);
            }

            int sum = 0;
            for(int l=1; l<=n; l++)
            {
                for(int r=l; r<=n; r++ )
                {
                    sum += funct(l, r, a, s);
                }
            }
            int res = sum % 998244353;
            Console.WriteLine(res);

        }

        public static int funct(int l, int r, int[] a, int s)
        {
            int bit_num = r - l + 1;
            int condition_num = (int)Math.Pow(2, bit_num);
            int count = 0;
            for(int i = 0; i < condition_num; i++)
            {
                int sum = 0;
                String twodig = Convert.ToString(i, 2).PadLeft(bit_num, '0');
                bool[] digits = twodig.Select(c => c=='1').ToArray();
                for(int j = 1; j<=bit_num; j++)
                {
                    if(digits[digits.Length - j])
                    {
                        sum += a[l - 1 + j - 1];
                    }
                }
                if(sum == s)
                {
                    // Console.WriteLine("f(" + l + ", " + r + ") :" + twodig);
                    count++;
                }
            }
            return count;



        }
    }
}