#include <bitset>
// START #include "main.h"
#if defined(PRINT_TIMING)
#include <chrono>
#endif
#include <iomanip>
#include <iostream>
#include <limits>
template <typename input_t>
struct solve_main_wrapper {
#if defined(MULTI_UNTIL)
#define _SOLVE_MAIN_LOOP_CONDITION true
using return_t = bool;
#else
#define _SOLVE_MAIN_LOOP_CONDITION testnum <= test_cases
using return_t = void;
#endif
input_t& cin;
solve_main_wrapper(input_t& _cin, int argc, char** argv) : cin(_cin) {
(void)argc;
(void)argv;
}
auto solve_main(size_t testnum) -> return_t;
auto solve_all() -> int {
[[maybe_unused]] size_t test_cases = 1;
#if defined(MULTI_TEST) or defined(PRINT_CASE)
cin >> test_cases;
#endif
for (size_t testnum = 1; _SOLVE_MAIN_LOOP_CONDITION; testnum++) {
#define MAKE_STRING_IMPL(STRING) #STRING
#define MAKE_STRING(STRING) MAKE_STRING_IMPL(STRING)
#if defined(PRINT_CASE)
std::cout << "Case " << MAKE_STRING(PRINT_CASE) << testnum << ": ";
#undef MAKE_STRING
#undef MAKE_STRING_IMPL
#endif
#if defined(PRINT_TIMING)
auto start_time = std::chrono::high_resolution_clock::now();
#endif
#if defined(MULTI_UNTIL)
if (not solve_main(testnum)) break;
#else
solve_main(testnum);
#endif
#if defined(PRINT_TIMING)
auto duration = std::chrono::high_resolution_clock::now() - start_time;
using namespace std::chrono;
std::cerr << "\n[t" << testnum << "] " << duration / 1.0s << "s\n\n";
#endif
}
return 0;
}
};
#if defined(FAST_INPUT) or defined(FAST_INPUT_BUFFER)
#include "utility/fast_input.h"
#endif
auto main(int argc, char** argv) -> int {
std::cout << std::fixed << std::setprecision(10);
#ifdef _USING_FAST_INPUT
#if not defined(FAST_INPUT_BUFFER)
#define FAST_INPUT_BUFFER 16384
#endif
fast_input<FAST_INPUT_BUFFER> cin;
solve_main_wrapper solver(cin, argc, argv);
#else
std::cin.tie(0)->sync_with_stdio(0);
solve_main_wrapper solver(std::cin, argc, argv);
#endif
return solver.solve_all();
}
#define SOLVE() \
template <typename input_t> \
auto solve_main_wrapper<input_t>::solve_main([[maybe_unused]] size_t testnum) -> return_t
using ll = long long;
constexpr char nl = '\n';
#include <cassert>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
// END #include "main.h"
SOLVE() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> order;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
order.emplace_back(a, i);
}
sort(begin(order), end(order));
static constexpr int N = 5e4;
bitset<N> builder;
vector<bitset<N>> bs(n);
for (auto [a, i] : order) {
bs[i] = builder;
bs[i] <<= n - i;
builder[i] = 1;
}
for (int i = 0; i < n; i++) {
bs[i] &= builder;
}
vector<bitset<N>> pref(n), suff(n);
for (int i = 0; i < n; i++) {
pref[i] = bs[i];
if (i % m != 0 and i - 1 >= 0) {
pref[i] &= pref[i - 1];
}
}
for (int i = n - 1; i >= 0; i--) {
suff[i] = bs[i];
if (i % m != m - 1 and i + 1 < n) {
suff[i] &= suff[i + 1];
}
}
ll ans = 0;
for (int i = 0; i + m - 1 < n; i++) {
auto res = suff[i] & pref[i + m - 1];
ans += res.count();
}
cout << ans << nl;
}