#include <bits/stdc++.h>
using namespace std;
long long int a[100005];
int main() {
  long long n, k = 1, sum = 0, d;
  cin >> n >> d;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[n + 1] = 1e18;
  for (long long i = 1; i <= n; i++) {
    if (k == i) {
      while (a[k] - a[i] <= d && k <= n) {
        k++;
      }
      sum += ((k - i) * (k - i - 1) * (k - i - 2)) / 6;
    } else {
      long long x = k;
      while (a[x] - a[i] <= d && x <= n) {
        x++;
      }
      sum += (((k - i) * (k - i - 1)) / 2) * (x - k);
      sum += (((x - k) * (x - k - 1)) / 2) * (k - i);
      sum += ((x - k) * (x - k - 1) * (x - k - 2)) / 6;
      k = x;
    }
  }
  cout << sum;
}
