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 (Python):

n,s = map(int,input().split())
a = list(map(int,input().split()))
dp = [[0]*(3001) for _ in range(3001)]

ans = 0
mod = 998244353
for i in range(n):
    dp[i][a[i]] = i+1
    if a[i] == s:
        ans += (i+1)*(n-i) %mod
for i in range(0,n):
    for j in range(s+1):
        if j > a[i]:
            dp[i][j] += dp[i-1][j] + dp[i-1][j-a[i]]
            dp[i][j] %= mod
            if j == s:
                ans += dp[i-1][j-a[i]] * (n-i)
                ans %= mod
                
        else:
            dp[i][j] += dp[i-1][j]
            dp[i][j] %= mod
print(ans%mod)

Solution (Python):
N, S = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
MOD =   998244353
DP = [[0 for p in range(S+1)]for x in range(N)]
ans = N * (A[0]==S)
if A[0]<=S:
    DP[0][A[0]] = 1
for n,a in enumerate(A):
    if n!=0:
        
        for p in range(a,S+1):
            DP[n][p] += DP[n-1][p-a]
        for p in range(S+1):
            DP[n][p] += DP[n-1][p]
            DP[n][p] %= MOD
        
        if a<=S:DP[n][a] += n+1
    ans += (DP[n][S]*(N-n))% MOD
    DP[n][S] = 0
    ans %= MOD
print(ans)

Solution (Python):
n,s = map(int, input().split())
a = list(map(int, input().split()))
mod = 998244353
dp = [[0] * (s+1) for i in range(n+1)]
for i in range(n):
  dp[i][0] = 1
for i in range(n):
  for j in range(s+1):
    dp[i+1][j] += dp[i][j]
    dp[i+1][j] %= mod
    if j >= a[i]:
      dp[i+1][j] += dp[i][j-a[i]]
      dp[i+1][j] %= mod
ans = list()
for i in dp:
  ans.append(i[-1])
ans = ans[1:]
print(sum(ans)%mod)

Solution (Python):
N, S = map(int, input().split())
A = [int(i) for i in input().split()]

mod = 998244353

dp = [[0] * (S + 1) for _ in range(N + 1)]

ret = 0
for i, a in enumerate(A) :
  if a <= S :
    dp[i + 1][a] += i + 1
    
  for j in range(S, -1, -1) :
    dp[i + 1][j] += dp[i][j]
    if j >= a :
      dp[i + 1][j] += dp[i][j - a]
    dp[i + 1][j] %= mod
        
  ret += (dp[i + 1][S] - dp[i][S]) * (N - i) % mod
  ret %= mod

print(ret)

Solution (Python):
n, s = map(int, input().split())
a = list(map(int, input().split()))

mod = 998244353
dp = [[0] * n for _ in range(3000 + 1)]
dp[a[0]][0] = 1

for i in range(1, s + 1):
    for j in range(1, n):
        if i == a[j]:
            dp[i][j] = dp[i][j-1] + j + 1
        elif i - a[j] > 0:
            dp[i][j] = dp[i][j-1] + dp[i - a[j]][j - 1]
        else:
            dp[i][j] = dp[i][j-1]

        dp[i][j] %= mod

print(sum([dp[s][j] for j in range(n)]) % mod)

Solution (Python):
n,s = map(int,input().split())
a = list(map(int,input().split()))
mod = 998244353
rui = []
now = 0
pl = []
for i in range(n):
    now += a[i]
    rui.append(now)
l = [[0 for i in range(s)] for i in range(n)]
ans = 0
for i in range(n):
    if a[i] < s:
        l[i][a[i]] += i+1
        l[i][a[i]] %= mod
    elif a[i] == s:
        ans += i*(n-i-1)
        ans += n
        ans %= mod
    for j in range(s):
        if i > 0:
            if l[i-1][j] > 0:
                if j+a[i] < s:
                    l[i][j+a[i]] += l[i-1][j]
                    l[i][j+a[i]] %= mod
                elif j+a[i] == s:
                    ans += l[i-1][j]*(n-i)
                    ans %= mod
                l[i][j] += l[i-1][j]
                l[i][j] %= mod
print(ans)

Solution (Python):
n, s = map(int, input().split())
a = list(map(int, input().split()))

mod = 998244353

dp = [[0] * n for _ in range(3000 + 1)]


dp[a[0]][0] = 1

for i in range(1, s + 1):
    for j in range(1, n):
        if i == a[j]:
            dp[i][j] = (dp[i][j-1] + j + 1) % mod
        elif i - a[j] > 0:
            dp[i][j] = (dp[i][j-1] + dp[i - a[j]][j - 1]) % mod
        else:
            dp[i][j] = dp[i][j-1] % mod



ans = 0
for j in range(n):
    ans += dp[s][j]

print(ans % mod)

Solution (Python):
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans
'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
'''




n,s = list(map(int, input().split()))
a = list(map(int, input().split()))

dp = [[0 for i in range(6002)]for j in range(n+1)]
ans = 0
for i in range(n):
    now = a[i]
    dp[i+1][now] = i+1
    for jj in range(s-1):
        j = jj+1
        dp[i+1][j] += dp[i][j]
        dp[i+1][j+now] += dp[i][j]
        dp[i+1][j] %= 998244353
    ans += dp[i+1][s] * (n - i)
    ans %= 998244353
    


print(ans)

Solution (Python):
n, s = map(int, input().split())
a = list(map(int, input().split()))
dp = [[k + 1] + [0] * s for k in range(n + 1)] 
for i in range(1, n + 1):
    for j in range(1, s + 1):
        if j - a[i - 1] < 0:
            dp[i][j] = dp[i - 1][j] % 998244353
        else:
            dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - a[i - 1]]) % 998244353
ans = 0
for i in range(1, n + 1):
    ans += dp[i][s]
    ans %= 998244353
print(ans)

Solution (Python):

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

from collections import Counter

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n, s = [int(s) for s in input().strip().split()]
    A = [int(s) for s in input().strip().split()]

    M = 998244353
    dp = Counter()
    dp[0] = 1
    result = 0
    for a in A:
        dp2 = Counter(dp)
        dp2[0] += 1
        for k, v in dp.items():
            if k + a <= s:
                dp2[k + a] = (dp2[k + a] + dp[k]) % M
        dp = dp2
        
        result = (result + dp[s]) % M
    print(result)




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

Solution (Python):
n,s = map(int, input().split())
a = list(map(int, input().split()))


dp = [[0] * (s + 1) for _ in range(n+1)]


for i in range(1, n+1):
    for j in range(1, s+1):
        if a[i-1] > j:
            dp[i][j] = dp[i-1][j]
        elif a[i-1] == j:
            dp[i][j] = (dp[i-1][j] + i)%998244353
        else:
            dp[i][j] = (dp[i-1][j] + dp[i-1][j-a[i-1]])%998244353

ans = 0

for i in range(1, n+1):
    ans += dp[i][s]
    ans %= 998244353


print(ans)

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 998244353
eps = 10**-7


def inp(): return int(sys.stdin.readline())


def inpl(): return list(map(int, sys.stdin.readline().split()))


def inpl_str(): return list(sys.stdin.readline().split())


N, S = inpl()
aa = inpl()

dp = [0 for _ in range(3010)]
dp[0] = 0

ans = 0
for i, a in enumerate(aa):

    if S == a:
        ans += (i+1) * (N-i) % mod
    else:
        ans += dp[S-a] * (N-i) % mod
    ans %= mod

    for s in reversed(range(a, S)):
        if s == a:
            dp[s] += i+1
        else:
            dp[s] += dp[s-a]
        dp[s] %= mod


print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

mod = 998244353
N, S = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
dp = [0] * (S+1)
for i in range(1, N+1):
    a = A[i-1]
    for j in range(a+1, S+1)[::-1]:
        dp[j] += dp[j-a]
    if a <= S:
        dp[a] += i
    ans += dp[S] * (N-i+1)
    ans %= mod
    dp[S] = 0
print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 998244353

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,S = LI()
A = LI()

d = defaultdict(int)
ans = 0
for i in range(N):
    temp = defaultdict(int)
    for k,v in d.items():
        temp[k] = v
    ans += d[S-A[i]]*(N-i)
    if S == A[i]:
        ans += (i+1)*(N-i)
    ans %= mod
    d[A[i]] += i+1
    for k in range(A[i]+1,3001):
        d[k] += temp[k-A[i]]
    
print(ans)

Solution (Python):
n,s = map(int,input().split())
a = list(map(int,input().split()))
dp1 = [[0 for i in range(s+1)] for j in range(n+1)]
dp2 = [[0 for i in range(s+1)] for j in range(n+1)]
mod = 998244353
dp1[0][0] = 1
for i in range(1,n+1):
  x = a[i-1]
  for j in range(1,s+1):
    p = 0
    q = 0
    if j == x:
      p += i
      q += i*(n-i+1)
    if j>x:
      dif = dp1[i-1][j-x]
      p = dif
      q = dif*(n+1-i)
    p += dp1[i-1][j]
    q += dp2[i-1][j]
    dp1[i][j] = p%mod
    dp2[i][j] = q%mod
print(dp2[n][s])

Solution (Python):
def main():
    n, s = map(int, input().split())
    a = list(map(int, input().split()))
    mod = 998244353

    dp = [[0]*3001 for _ in [0]*n]
    dp[0][a[0]] = 1
    for j, i in enumerate(a[1:]):
        dp[j+1][i] += (j+2)
        for k in range(3001-i):
            dp[j+1][i+k] = (dp[j+1][i+k]+dp[j][k]) % mod
        for k in range(3001):
            dp[j+1][k] = (dp[j+1][k]+dp[j][k]) % mod
    print(sum([dp[i][s] for i in range(n)]) % mod)


main()

Solution (Python):
N, S = map(int, input().split())
A = [int(a) for a in input().split()]
X = [0] * 3001
pre = 0
ans = 0
P = 998244353
for i, a in enumerate(A):
    for j in range(S, a, -1):
        X[j] = (X[j] + X[j-a]) % P
    X[a] += i + 1
    ans += (X[S] - pre) * (N - i)
    pre = X[S]
print(ans % P)

Solution (Python):
import sys
input = sys.stdin.readline
n, s = map(int,input().split())
A = list(map(int,input().split()))
mod = 998244353

ans = 0
B = [0] * (s+1)
for i in range(n):
    for j in range(s, -1, -1):
        if j-A[i] >= 0:
            B[j] = (B[j] + B[j-A[i]])%mod
    if A[i] < len(B):
        B[A[i]] += i+1
    ans = (ans + B[s])%mod
    
    
print(ans)

Solution (Python):
n, s = map(int, input().split())
a = list(map(int, input().split()))

dp = [[1 if _2 == 0 else 0 for _2 in range(s + 1)] for _1 in range(n+1)]

MOD = 998244353

for i, ai in enumerate(a):
  for j, v in enumerate(dp[i]):
    dp[i + 1][j] += v
    dp[i + 1][j] %= MOD 
    if j + ai <= s:
      dp[i + 1][j + ai] += v
      dp[i + 1][j + ai] %= MOD


print(sum(dp_i[s] for dp_i in dp) % MOD)

Solution (Python):
import sys
input = sys.stdin.readline
N, S = map(int, input().split())
a = list(map(int, input().split()))
mod = 998244353

dp = [[0] * (S + 1) for _ in range(N + 1)]
dp[0][0] = 1
for i in range(N):
  dp[i][0] = i + 1
  for j in range(S + 1):
    if j + a[i] < S:
      dp[i + 1][j + a[i]] += dp[i][j]
      dp[i + 1][j + a[i]] %= mod
    elif j + a[i] == S:
      dp[i + 1][j + a[i]] += dp[i][j] * (N - i)
      dp[i + 1][j + a[i]] %= mod
    dp[i + 1][j] += dp[i][j]
    dp[i + 1][j] %= mod
print(dp[-1][-1])

Solution (Python):
n, s = map(int, input().split())
a = [int(x) for x in input().split()]
mod = 998244353

dp = [[0 for _ in range(s+1)] for __ in range(n+1)]

dp[0][0] = 1

for i in range(n):
    for j in range(s+1):
        dp[i+1][j] += dp[i][j]
        if j-a[i] == 0:
            dp[i+1][j] += i+1
        elif j-a[i] >= 0:
            dp[i+1][j] += dp[i][j-a[i]]
        dp[i+1][j] %= mod

ans = 0

for i in range(n+1):
    ans += dp[i][s]
    ans %= mod

print(ans)

Solution (Python):
N,S=map(int,input().split())
A=list(map(int,input().split()))
mod=998244353




dp=[0]*S
dp[0]=1
ans=0

for i in range(N):
    h=dp[:]
    a=A[i]
    if a>S:
        continue
    if a==S:
        ans=(ans+(i+1)*(N-i))%mod
        continue
        
    h[a]=(h[a]+i+1)%mod
    for j in range(a+1,S):
        h[j]=(h[j]+dp[j-a])%mod
    ans=(ans+dp[S-a]*(N-i))%mod
    dp=h
    
print(ans)

Solution (Python):
N, S = map(int, input().split())
A = [int(a) for a in input().split()]
X = [0] * 3001
pre = 0
ans = 0
P = 998244353
for i, a in enumerate(A):
    for j in range(S, a, -1):
        X[j] = (X[j] + X[j-a]) % P
    X[a] = (X[a] + i + 1) % P
    ans = (ans + (X[S] - pre) * (N - i)) % P
    pre = X[S]
print(ans)

Solution (Python):
from numpy import *
M=998244353
N,S,*A=map(int,open(0).read().split())
a=0
f=zeros(S+1,int)
for b in A:
  f[0]+=1
  f[b:] += f[:-b].copy()
  f%=M
  a+=f[S]
print(a%M)

Solution (Python):


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


n,s,*a = [int(i) for i in read().split()]
MOD = 998244353
 

def mul(c,m,dp): 
    for i in range(m,c-1,-1):
        dp[i] += dp[i-c]
        dp[i] %= MOD
 
dp = [0]*(s+1)

ans = 0
for c in a:
    dp[0] += 1
    mul(c,s,dp)
    ans += dp[s]
    

print(ans%MOD)

Solution (Python):
n, s = map(int, input().split())
a = list(map(int, input().split()))
MOD = 998244353

dp = [0] * (3000 + 1)
dp[0] = 1
ans = 0
for i in range(n):
    for w in range(3000 + 1)[::-1]:
        
        if w - a[i] > 0:
            dp[w] += dp[w - a[i]]
        if w - a[i] == 0:
            dp[w] += dp[w - a[i]] * (i + 1)
        dp[w] %= MOD
    ans += dp[s]
    ans %= MOD
print(ans % MOD)

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

MOD = 998244353

N, S, *As = map(int, read().split())

U = 3010
answer = 0
f = np.zeros(U, np.int64)
for A in As:
    f[0] += 1
    f[A:] += f[:-A].copy()
    f %= MOD
    answer += f[S]

answer %= MOD
print(answer)

Solution (Python):
N,S=map(int,input().split())
A=list(map(int,input().split()))
mod=998244353

dp=[[0 for i in range(0,S+1)] for j in range(0,N)]
for i in range(0,S+1):
    if i==A[0]:
        dp[0][i]=1

for i in range(1,N):
    for j in range(0,S+1):
        if j>A[i]:
            dp[i][j]=(dp[i-1][j]+dp[i-1][j-A[i]])%mod
        elif j==A[i]:
            dp[i][j]=(dp[i-1][j]+(i+1))%mod
        else:
            dp[i][j]=dp[i-1][j]

ans=0
for i in range(0,N):
    ans=(ans+dp[i][S])%mod

print(ans)

Solution (Python):
n, s = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
mod = 998244353

dp = [[0] * (s + 1) for _ in range(n + 1)]
dp[0][0] = 1

for i in range(n):
    dp[i][0] = i + 1
    for j in range(1, s + 1):
        if j >= a[i]:
            dp[i+1][j] += dp[i][j-a[i]]
        dp[i + 1][j] += dp[i][j]
        dp[i + 1][j] %= mod

ans = 0
for i in range(1, n + 1):
    ans += dp[i][-1] * 1
    ans %= mod
print(ans)

Solution (Python):
n, s = map(int, input().split())
a = list(map(int, input().split()))

MOD = 998244353

dp = [[0 for _ in range(s + 1)] for _ in range(n + 1)]

ans = 0

for i, x in enumerate(a):
    if x < s:
        dp[i+1][x] = i + 1
    if x == s:
        ans = (ans + (n-i) * (i+1)) % MOD
    for j in range(1, s):
        if dp[i][j] > 0:
            dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % MOD
            if j + x < s:
                dp[i+1][j+x] = (dp[i+1][j+x] + dp[i][j]) % MOD
            elif j + x == s:
                ans = (ans + dp[i][j] * (n-i)) % MOD

print(ans)

Solution (Python):
mod=998244353
n,s=map(int,input().split())
a=list(map(int,input().split()))

dp=[[0]*(s+1) for _ in range(n)]

if a[0]<=s: dp[0][a[0]]=1

for i in range(1,n):
    for k in range(s+1):
        dp[i][k]=dp[i-1][k]
        if k-a[i]>0: dp[i][k]+=dp[i-1][k-a[i]]
        if a[i]==k: dp[i][k]+=i+1
        dp[i][k]%=mod

ans=0
for i in range(n):
    ans+=dp[i][s]
    ans%=mod
print(ans)

Solution (Python):
def main():
    import sys
    input=sys.stdin.readline
    n,s=map(int,input().split())
    A=list(map(int,input().split()))
    mod=998244353
    dp=[[0]*(s+1) for _ in range(n+1)]
    dp[0][0]=1
    for i in range(1,n+1):
        for j in range(s+1):
            a=A[n-i]
            if j-a>0:
                dp[i][j]=(dp[i-1][j]+dp[i-1][j-a])%mod
            elif j==a:
                dp[i][j]=(dp[i-1][j]+i)%mod
            else:
                dp[i][j]=dp[i-1][j]
    ans=0
    for i in range(1,n+1):
        a=A[i-1]
        if s>a:
            ans=(ans+dp[n-i][s-a]*i)%mod
        elif s==a:
            ans=(ans+(n-i+1)*i)%mod
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):

N,S = map(int,input().split())

A = list(map(int,input().split()))

"""
dpなのはそうだろう
普通に行けば、ナップサックdpでいける
S以下のみしか保持しなくていい

始点を保持してしまうと、Sになった時に…？
始点より左にあるやつの個数の和を記録しておけばいい！

連続してなくていいから
"""

dp = [0] * (S+1)
ans = 0
mod = 998244353

for i in range(N):

    ndp = []
    for ll in dp:
        ndp.append(ll)

    if A[i] <= S:
        ndp[A[i]] += i+1
        ndp[A[i]] %= mod

    for j in range(S):

        if j+A[i] <= S:
            ndp[j+A[i]] += dp[j]
            ndp[j+A[i]] %= mod

    ans += ndp[S]
    ans %= mod

    dp = ndp

    

    
print (ans)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, S = na()
a = na()
mod = 998244353

def go(f, t, step, a, S):
    dp = [0] * (S+1)
    dp[0] = 1
    ret = [0] * (S+1)
    while f != t:
        for i in range(S, a[f]-1, -1):
            dp[i] += dp[i-a[f]]
            if dp[i] >= mod:
                dp[i] -= mod
        for i in range(S+1):
            ret[i] += dp[i]
        f += step
    for i in range(S+1):
        ret[i] %= mod
    return ret

def dfs(l, r, a, S):
    if r-l <= 1:
        return 1 if a[l] == S else 0
    h = (l+r)//2
    L = go(h-1, l-1, -1, a, S)
    R = go(h, r, 1, a, S)
    
    return sum(L[i] * R[S-i] for i in range(S+1)) + dfs(l, h, a, S) + dfs(h, r, a, S)

print(dfs(0, n, a, S) % mod)