import sys from collections import defaultdict from heapq import heappop, heappush read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N, K, *A = map(int, read().split()) def add(i): x = A[i] heappush(q, x) counter[x] += 1 index_sum[x] += i def remove(i): x = A[i] counter[x] -= 1 index_sum[x] -= i # 最小元がqに存在することを保証 while q and counter[q[0]] == 0: heappop(q) q = [] counter = defaultdict(int) index_sum = defaultdict(int) # 左端 L を固定 # 極大な[L,R]を求める answer = 0 L = R = -1 for L in range(N): while R < N - 1: x = A[R + 1] if q and q[0] == x: break R += 1 add(R) answer += index_sum[K - A[L]] - (L - 1) * counter[K - A[L]] remove(L) print(answer)