Problem:
Score : 700 points

Problem Statement

Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).

Now, he will perform the following operation exactly once:

Choose K consecutive elements in P and sort them in ascending order.

Find the number of permutations that can be produced as P after the operation.

Constraints

2 \leq N \leq 200000

2 \leq K \leq N

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_0 P_1 \cdots P_{N-1}

Output

Print the number of permutations that can be produced as P after the operation.

Sample Input 1

5 3
0 2 1 4 3

Sample Output 1

2

Two permutations can be produced as P after the operation: (0,1,2,4,3) and (0,2,1,3,4).

Sample Input 2

4 4
0 1 2 3

Sample Output 2

1

Sample Input 3

10 4
2 0 1 3 7 5 4 6 8 9

Sample Output 3

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> order_set;
const ll mod = 1e9 + 7, p = 201101, inv = 772765929;
const ll N = 2e5 + 5, M = 129;
const ld pi = acos(-1);
ll pw[N];
ll t[4*N], lazy[4*N];
order_set st;
    
ll power(ll x, unsigned ll y, ll p)
{
        ll res = 1; 
        x = x % p;
        while (y > 0)
        {
            if (y & 1)
                res = (res*x) % p;
            y = y>>1;
            x = (x*x) % p;  
        }
    return res;
}

void update(ll node,ll start,ll end,ll idx,ll value)
{
    if(start==end)
    {
        t[node]=value;
    }
    else
    {
        ll mid=(start+end)/2;
        if(idx>=start&&idx<=mid)
        {
            update(2*node,start,mid,idx,value);
        }
        else
        {
            update(2*node+1,mid+1,end,idx,value);
        }
        t[node] = (t[2*node]+t[2*node+1])%mod;
    }
}

void updateRange(ll node, ll start, ll end, ll l, ll r, ll val)
{
    if(lazy[node] != 1)
    { 
        t[node] = (t[node]*lazy[node])%mod;    
        if(start != end)
        {
            lazy[node*2] = (lazy[2*node]*lazy[node])%mod;                  
            lazy[node*2+1] = (lazy[2*node+1]*lazy[node])%mod;               
        }
        lazy[node] = 1;                                  
    }
    if(start > end or start > r or end < l)             
        return;
    if(start >= l and end <= r)
    {
        t[node] = (t[node]*val)%mod;
        if(start != end)
        {
            lazy[node*2] = (lazy[2*node]*val)%mod;
            lazy[node*2+1] = (lazy[2*node+1]*val)%mod;
        }
        return;
    }
    ll mid = (start + end) / 2;
    updateRange(node*2, start, mid, l, r, val);        
    updateRange(node*2 + 1, mid + 1, end, l, r, val);  
    t[node] = (t[node*2] + t[node*2+1])%mod;     
}

ll queryRange(ll node, ll start, ll end, ll l, ll r)
{       
    if(lazy[node] != 1)
    { 
        t[node] = (t[node]*lazy[node])%mod;    
        if(start != end)
        {
            lazy[node*2] = (lazy[2*node]*lazy[node])%mod;                  
            lazy[node*2+1] = (lazy[2*node+1]*lazy[node])%mod;               
        }
        lazy[node] = 1;                                  
    }
    if(start > end or start > r or end < l)
        return 0;  
    if(start >= l and end <= r)            
        return t[node];
    ll mid = (start + end) / 2;
    ll p1 = queryRange(node*2, start, mid, l, r);        
    ll p2 = queryRange(node*2 + 1, mid + 1, end, l, r); 
    return (p1 + p2)%mod;
}
    
int main() 
{ 
    FAST;
    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
    pw[0] = 1;
    for(ll i=1;i<N;i++) {
        pw[i] = (pw[i-1]*p)%mod;
    }
    ll n, k;
    cin >> n >> k;
    ll a[n], pre[n+1];
    pre[0] = 0;
    for(ll i=0;i<n;i++) {
        cin >> a[i];
        a[i]++;
        pre[i+1] = (pre[i] + a[i]*pw[i])%mod; 
    }
    set <ll> s;
    for(ll i=0;i<4*N;i++) {
        t[i] = 0;
        lazy[i] = 1;
    }
    vector <ll> v;
    for(ll i=0;i<k;i++) {
        v.pb(a[i]);
    }
    sort(v.begin(), v.end());
    for(ll i=0;i<k;i++) {
        update(1, 1, n, v[i], (v[i]*pw[i])%mod);
        st.insert(v[i]);
    }
    ll x = queryRange(1, 1, n, 1, n);
    x = (x + pre[n] - pre[k] + mod)%mod;
    s.insert(x);
    //cout << 0 << " " << x << endl;
    for(ll i=k;i<n;i++) {
        st.erase(a[i-k]);
        if(a[i-k] != n)
            updateRange(1, 1, n, a[i-k] + 1, n, inv);
        updateRange(1, 1, n, a[i-k], a[i-k], 0);
        st.insert(a[i]);
        ll idx = st.order_of_key(a[i]);
        if(a[i] != n)
            updateRange(1, 1, n, a[i] + 1, n, p);
        updateRange(1, 1, n, a[i], a[i], 1);
        update(1, 1, n, a[i], (a[i]*pw[idx])%mod);
        x = queryRange(1, 1, n, 1, n);
        ll z =  (pre[i-k+1] + pre[n] - pre[i+1] + (x*pw[i-k+1])%mod)%mod;
        z = (z + mod)%mod;
        s.insert(z);
        //cout << i-k+1 << " " << z << endl;
    }
    cout << s.size();
    return 0;
}