stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <cstring>

#include <cmath>

#include <algorithm>

#define ll long long

using namespace std;



ll arr[100005];

int memo[100005];



int main()

{

    int n;

    cin >> n;

    

    memset (memo,0,sizeof(memo));

    int ans = 1;

    int track = 2;

    //if track = 0 it means that it is increasing, track = 1 means it is decreasing track = 2 is neutral

    for (int i = 1; i <= n; i++){

        cin >> arr[i];

        if (i >= 2){

          if (track == 2 && arr[i] != arr[i-1]){

            if (arr[i] > arr[i-1])

              track = 0;

            else

              track = 1;

          }

          else if (track != 2){

            if (arr[i] > arr [i-1] && track == 1){

              ans++;

              track = 2;

            }

            else if (arr[i] < arr [i-1] && track == 0){

              ans++;

              track = 2;

            }

          }

        }

    }

    

    

    

    cout << ans << endl;



    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

int n , x[200000] , pos , ans , now;

int main(){

	cin >> n;

	for(int i = 1 ; i <= n ; i++){

		cin >> x[i];

	}

	now = 0;

	for(int i = 2 ; i <= n ; i++){

		if(now == 0){

			if(x[i] == x[i - 1])now = 0;

			if(x[i] > x[i - 1])now = 1;

			if(x[i] < x[i - 1])now = 2;

		}

		else if(now == 1){

			if(x[i] < x[i - 1]){

				now = 0;

				ans++;

			}

		}

		else if(now == 2){

			if(x[i] > x[i - 1]){

				now = 0;

				ans++;

			}

		}

	}

	cout << ans + 1 << endl;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

struct node

{

    int x,note=0x3f;

}a[100005]; 

int main()

{ 

    int n,ans=1;

    cin>>n; 

    for(int i=1;i<=n;i++)

	{

        cin>>a[i].x;

    }

    a[1].note=2;

    for(int i=2;i<=n;i++)

	{

        if(a[i-1].note==0)

		{

            if(a[i].x>a[i-1].x) a[i].note=2,ans++;

              else a[i].note=0;

        }

		else if(a[i-1].note==1)

		{

            if(a[i].x>=a[i-1].x) a[i].note=1;

              else a[i].note=2,ans++;

        }

		else if(a[i-1].note==2)

		{

            if(a[i].x<a[i-1].x) a[i].note=0;

              else if(a[i].x==a[i-1].x) a[i].note=2;

                else a[i].note=1;

        }

    }

    cout<<ans<<endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int n;

const int N=100005;

int a[N],f[N],ans=1;

int main(){

    scanf("%d",&n);

    for(int i=1;i<=n;i++) scanf("%d",&a[i]);

    f[1]=0;

    for(int i=2;i<=n;i++){

        if(f[i-1]==1){

            if(a[i-1]>a[i]) f[i]=0,ans++;

            else f[i]=1;

        }

        else if(f[i-1]==2){

            if(a[i-1]<a[i]) f[i]=0,ans++; 

            else f[i]=2;

        }

        else{

            if(a[i-1]>a[i]) f[i]=2;

            else if(a[i-1]<a[i]) f[i]=1;

            else f[i]=0;

        }

    }

    printf("%d\n",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define FOR(i,a,b) for(int i = (a); i < (b); i++)

#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--)

#define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define FILL(a, value) memset(a, value, sizeof(a))



#define SZ(a) (int) a.size()

#define ALL(a) a.begin(),a.end()

#define PB push_back

#define MP make_pair



typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;



const double PI = acos(-1.0);

const LL INF = 1000 * 1000 * 1000 + 7;

const LL LINF = INF * (LL)INF;



const int MAX = 100100;



int A[MAX];



int main()

{

	//freopen("in.txt", "r", stdin);

	//ios::sync_with_stdio(false); cin.tie(0);



	int n;

	scanf("%d", &n);

	FOR (i, 0, n)

	{

		scanf("%d", &A[i]);

	}



	if (n == 1)

	{

		cout<<1<<endl;

		return 0;

	}





	int res = 1;

	int t = -1;

	FOR (i, 1, n)

	{

		if (t == -1)

		{

			if (A[i] > A[i-1]) t = 0;

			if (A[i] < A[i-1]) t = 1;

		}



		if (t != -1)

		{

			if (A[i] >= A[i-1] && t == 0) continue;

			if (A[i] <= A[i-1] && t == 1) continue;



			res++;

			t = -1;

		}

	}

	cout<<res<<endl;



}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long LL;

using namespace std;

#define max_n 100005

int a[max_n];

int main()

{

	int n,k=1,ans,i;

	bool sign,res=false;

	scanf("%d",&n);

	scanf("%d",&a[0]);

	if(n==1)

	{

		printf("1\n");

		return 0;

	}

	ans=a[0];

	for(i=1;i<n;i++)

	{

		scanf("%d",&a[i]);

		if(!res)

		{

			if(a[i]>ans)

			{

				sign=true;

				res=true;

				ans=a[i];

			}

			else if(a[i]<ans)

			{

				sign=false;

				res=true;

				ans=a[i];

			}

		}

		else

		{

			if(sign)

			{

				if(a[i]>=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

			else

			{

				if(a[i]<=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

		}

	}

	printf("%d\n",k);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

const int INF = 1e9;

const ll LINF = 1e18;



/*

<url:https://agc013.contest.atcoder.jp/tasks/agc013_a>

問題文============================================================

長さ N の配列 A が与えられます。

 A を何箇所かで切って、A の連続した部分であるようないくつかの数列に分けます。

 この時、分けられたあとの数列は全て、単調非減少または単調非増加な列になっている必要があります。

 最小で何個の数列に分ければ良いかを求めて下さい。

=================================================================



解説=============================================================



 先に、隣り合う値が等しい部分に関しては、処理を簡単にするために、unique等で

 削除しておくと楽

 

 あとは、順番に現在 単調増加なのか減少なのか、または確定できないのか

 といったフラグを持っておき、実際にシミュレーションしていく

================================================================

*/

int main(void) {

	cin.tie(0); ios::sync_with_stdio(false);

    ll N; cin >> N;

    vector<ll> A(N); for(auto& in:A) cin >> in;

    A.erase(unique(A.begin(),A.end()),A.end());

    int mode = 0;

    int ans = 1;

    for(int i = 0 ; i < (int)A.size()-1;i++){

        if(mode == 0){

            if(A[i] < A[i+1]) mode = 1;

            else mode = -1;

        }else{

            if(mode == 1){

                if(A[i] > A[i+1]){

                    ans++;

                    mode = 0;

                }

            }else{

                if(A[i] < A[i+1]){

                    ans++;

                    mode = 0;

                }

            }

        }

    }

    cout << ans << endl;

	return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long LL;

using namespace std;

#define max_n 100005

int a[max_n];

int main()

{

	int n,k=1,ans,i;

	bool sign,res=false;

	scanf("%d",&n);

	scanf("%d",&a[0]);

	if(n==1)

	{

		printf("1\n");

		return 0;

	}

	ans=a[0];

	for(i=1;i<n;i++)

	{

		scanf("%d",&a[i]);

		if(!res)

		{

			if(a[i]>ans)

			{

				sign=true;

				res=true;

				ans=a[i];

			}

			else if(a[i]<ans)

			{

				sign=false;

				res=true;

				ans=a[i];

			}

		}

		else

		{

			if(sign)

			{

				if(a[i]>=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

			else

			{

				if(a[i]<=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

		}

	}

	printf("%d\n",k);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstring>

using namespace std;

int pd[100010],arr[100010];

int main()

{

    int n,m,ans=0,T=0;

    memset(pd,0,sizeof(pd));

    cin>>n;

    

    for(int i=1;i<=n;i++)

    {

        cin>>arr[i];

    }



    T=arr[2]-arr[1];

    for (int i=3; i<=n; i++) {

        if(T==0)T=arr[i]-arr[i-1];

        if(1LL* T * (arr[i]-arr[i-1])<0){

            ans++;

            T=0;

        }

    }

    

    cout<<ans+1<<endl;

}

// Optimized solution to the same problem (faster):


#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long LL;

using namespace std;

#define max_n 100005

 

int a[max_n];

 

int main()

{

	int n,k=1,ans,i;

	bool flag,res=false;

	scanf("%d",&n);

	scanf("%d",&a[0]);

	if(n==1)

	{

		printf("1\n");

		return 0;

	}

	ans=a[0];

	for(i=1;i<n;i++)

	{

		scanf("%d",&a[i]);

		if(!res)

		{

			if(a[i]>ans)

			{

				flag=true;

				res=true;

				ans=a[i];

			}

			else if(a[i]<ans)

			{

				flag=false;

				res=true;

				ans=a[i];

			}

		}

		else

		{

			if(flag)

			{

				if(a[i]>=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

			else

			{

				if(a[i]<=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

		}

	}

	printf("%d\n",k);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<iostream>

#include<algorithm>

using namespace std;

const int maxn=1e5+5;

int a[maxn]={0};

int main()

{

	int n,t=0,f=1,sum=0;

	cin>>n;

	while(n--)

	{

		long long x;

		cin>>x;

		if(t==0)

		{

			f=1;

			a[t]=x;

			t++;

		}

		else if(t==1)

		{

			if(a[t-1]==x)

				t=0;

			else if(a[t-1]<x)

				f=1;

			else if(a[t-1]>x)

				f=-1;

			a[t]=x;

			t++;

		}

		else

		{

			if(f==1)

			{

				if(a[t-1]>x)

				{

					sum++;

					t=0;

					a[t]=x;

					t++;

				}

				else

				{

					a[t]=x;

					t++;

				}

			}

			else

			{

				if(a[t-1]<x)

				{

					sum++;

					t=0;

					a[t]=x;

					t++;

				}

				else

				{

					a[t]=x;

					t++;

				}

			}

		}

	}

	cout<<sum+1<<endl;

}

// Optimized solution to the same problem (faster):


#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long LL;

using namespace std;

#define max_n 100005

 

int a[max_n];

 

int main()

{

	int n,k=1,ans,i;

	bool flag,res=false;

	scanf("%d",&n);

	scanf("%d",&a[0]);

	if(n==1)

	{

		printf("1\n");

		return 0;

	}

	ans=a[0];

	for(i=1;i<n;i++)

	{

		scanf("%d",&a[i]);

		if(!res)

		{

			if(a[i]>ans)

			{

				flag=true;

				res=true;

				ans=a[i];

			}

			else if(a[i]<ans)

			{

				flag=false;

				res=true;

				ans=a[i];

			}

		}

		else

		{

			if(flag)

			{

				if(a[i]>=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

			else

			{

				if(a[i]<=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

		}

	}

	printf("%d\n",k);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int a[100005];

int main()

{

	int n,k=1,anss,i;

	bool flag,ress=false;

	scanf("%d",&n);

	scanf("%d",&a[0]);

	if(n==1)

	{

		printf("1\n");

		return 0;

	}

	anss=a[0];

	for(i=1;i<n;i++)

	{

		scanf("%d",&a[i]);

		if(!ress)

		{

			if(a[i]>anss)

			{

				flag=true;

				ress=true;

				anss=a[i];

			}

			else if(a[i]<anss)

			{

				flag=false;

				ress=true;

				anss=a[i];

			}

		}

		else

		{

			if(flag)

			{

				if(a[i]>=anss)

					anss=a[i];

				else

				{

					k++;

					anss=a[i];

					ress=false;

				}

			}

			else

			{

				if(a[i]<=anss)

					anss=a[i];

				else

				{

					k++;

					anss=a[i];

					ress=false;

				}

			}

		}

	}

	cout<<k<<endl;

	

}


// Optimized solution to the same problem (faster):


#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long LL;

using namespace std;

#define max_n 100005

 

int a[max_n];

 

int main()

{

	int n,k=1,ans,i;

	bool flag,res=false;

	scanf("%d",&n);

	scanf("%d",&a[0]);

	if(n==1)

	{

		printf("1\n");

		return 0;

	}

	ans=a[0];

	for(i=1;i<n;i++)

	{

		scanf("%d",&a[i]);

		if(!res)

		{

			if(a[i]>ans)

			{

				flag=true;

				res=true;

				ans=a[i];

			}

			else if(a[i]<ans)

			{

				flag=false;

				res=true;

				ans=a[i];

			}

		}

		else

		{

			if(flag)

			{

				if(a[i]>=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

			else

			{

				if(a[i]<=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

		}

	}

	printf("%d\n",k);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;

long long  a[100005];

int main()

{

	int n;

	cin>>n;

	int i,j;

	for(i=0;i<n;i++)

	cin>>a[i];

	int count=1;

	int flag;

	for(i=0;i<n;i=j)

		for(j=i+1;j<n;j++)

			{

				if(i==n-1)

				{

				i++;

				count++;

				break;	

				}

				if(a[i]==a[i+1])

				break;

				else if(a[i]<a[i+1])

				flag=1;

				else if(a[i]>a[i+1])

				flag=0;

				if(flag)

				{

					if(a[j]>=a[j-1])

					continue;

					else {

						count++;

						break;

					}

				}

				else {

					if(a[j]<=a[j-1])

					continue;

					else {

						count++;

						break;

					}

				}

			}

			cout<<count;

			return 0;

	

}

// Optimized solution to the same problem (faster):


#include<cstdio>

#include<cstring>

#include<algorithm>

typedef long long LL;

using namespace std;

#define max_n 100005

 

int a[max_n];

 

int main()

{

	int n,k=1,ans,i;

	bool flag,res=false;

	scanf("%d",&n);

	scanf("%d",&a[0]);

	if(n==1)

	{

		printf("1\n");

		return 0;

	}

	ans=a[0];

	for(i=1;i<n;i++)

	{

		scanf("%d",&a[i]);

		if(!res)

		{

			if(a[i]>ans)

			{

				flag=true;

				res=true;

				ans=a[i];

			}

			else if(a[i]<ans)

			{

				flag=false;

				res=true;

				ans=a[i];

			}

		}

		else

		{

			if(flag)

			{

				if(a[i]>=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

			else

			{

				if(a[i]<=ans)

					ans=a[i];

				else

				{

					k++;

					ans=a[i];

					res=false;

				}

			}

		}

	}

	printf("%d\n",k);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<iostream>

using namespace std;

int n,ans=1,m[100066];

int main(){

	cin>>n;

	bool jia=false,jian=false,flag=false;

	for(int i=1;i<=n;i++)cin>>m[i];

	for(int i=1;i<=n;i++){

		if(jia==false&&jian==false&&m[i]!=m[i+1]){

			if(m[i+1]>m[i])jia=true;

			else if(m[i+1]<m[i])jian=true;

		}

		if(i!=1) {

			if(m[i]==m[i-1])continue;

			else if(m[i]>m[i-1]){

				if(jia==false&&jian==true){

					ans++;jian=false;

				}

			}

			else if(m[i]<m[i-1]){

				if(jian==false&&jia==true){

					ans++;jia=false;

				}

			}

		}

		if(jia==false&&jian==false&&m[i]!=m[i+1]){

			if(m[i+1]>m[i])jia=true;

			else if(m[i+1]<m[i])jian=true;

		}

	}

	cout<<ans;

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

int n, a[100010];

int main() {

	scanf("%d", &n);

	for (int i = 1; i <= n; ++ i) scanf("%d", &a[i]);

	int ans = 1, pre = a[1], how = 0;

	for (int i = 2; i <= n; ++ i) {

		if (a[i] == pre) {

			continue ;

		}

		if (a[i] < pre) {

			if (how == 1) {

				++ ans;

				pre = a[i];

				how = 0;

			} else {

				how = -1;

				pre = a[i];

			}

		}

		if (a[i] > pre) {

			if (how == -1) {

				++ ans;

				pre = a[i];

				how = 0;

			} else {

				how = 1;

				pre = a[i];

			}

		}

	}

	printf("%d\n", ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cmath>

#include<cstring>

using namespace std;

struct node{

    int x,note=0x3f;//x代表数字，note表示状态，是上升还是下降

    //note=0，下降序列；note=1，上升序列；note=2，起点 或 起点后不明上下的点 

}a[100005]; //声明一个数组 

int main(){//主函数 

    int n,ans=1;//n个数，ans统计答案 

    cin>>n;//输入个数 

    for(int i=1;i<=n;i++){//挨个输入 

        cin>>a[i].x;//把数输入 

    }

    a[1].note=2;//第一个数必然是起点，打好状态标记 

    for(int i=2;i<=n;i++){//从第二个数开始找状态 

        if(a[i-1].note==0){//前一个数状态为0（下降） 

            if(a[i].x>a[i-1].x) a[i].note=2,ans++;//如果不下降了，这个数就是下一个序列的起点，序列数++ 

            else a[i].note=0;//下降，标记成0 

        }else if(a[i-1].note==1){//前一个数状态为1（上升） 

            if(a[i].x>=a[i-1].x) a[i].note=1;//继续上升，标记成1 

            else a[i].note=2,ans++;//不上升了，标记成起点，序列数++ 

        }else if(a[i-1].note==2){//是起点或起点后的不明点（统一当起点即可） 

            if(a[i].x<a[i-1].x) a[i].note=0;//比前一个数小，证明是下降序列，标为0 

            else if(a[i].x==a[i-1].x) a[i].note=2;//相等，这个点是不明上升/下降的点 

            else a[i].note=1;//上升，标成1 

        }

    }

    cout<<ans<<endl;//输出（换行） 

    return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <cstdio>

using namespace std;

typedef long long ll;

ll x=0,n,last,sum=1;

inline void f(ll k){

	if(x==0){

		if(k>last)x=1;

		else if(k<last)x=2;

	}

	else{

		if(k>last&&x==2)sum++,x=0;

		else if(k<last&&x==1)sum++,x=0;

	}

}

int main(){

	register int i,j;

	scanf("%lld",&n);

	for(i=1;i<=n;i++){

		ll k;

		scanf("%lld",&k);

		if(i>1)f(k);

		last=k;

	}

	cout<<max(sum,(ll)1)<<endl;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



const int MAXN = 100005;



int A[MAXN];



int N, Ans;



int main() {

	ios::sync_with_stdio(false);



	cin >> N;

	for(int i = 1; i <= N; i++)

		cin >> A[i];



	{

		int i = 0;



		for(; i <= N;) {

			bool isu = true, isd = true;

			for(i += 2; i <= N; i++) {

				if(A[i-1] == A[i]) continue;

				if(A[i-1] < A[i]) {

					isd = false;

				} else {

					isu = false;

				}

				if(!isd && !isu) { i--; break; }

			}

			if(N < i) break;

			Ans++;

		}

	}



	cout << (Ans+1) << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[121212];

int main() {

	int n, i, j, k, ans = 0; scanf("%d", &n);

	for (i = 0; i < n; i++)scanf("%d", &a[i]);

	for (i = 0; i < n; ) {

		for (j = i + 1; j < n&&a[j - 1] <= a[j]; j++);

		for (k = i + 1; k < n&&a[k - 1] >= a[k]; k++);

		i = (j > k ? j : k);

		ans++;

	}

	printf("%d", ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream> // cin, cout, cerr

#include <algorithm> // minmax, sort, swap

#include <numeric> // iota

#include <cstdio> // printf, scanf

#include <string> // string, stoi, to_string

#include <vector> // vector

#include <queue> // queue, priority_queue

#include <deque> // deque

#include <map> // key-value pairs sorted by keys

#include <set> // set

#include <iomanip> // cout<<setprecision(n)

#include <functional> // function<void(int)>

#include <cmath>

#include <cassert>

#include <bitset>



#ifdef DEBUG

#include "debug.hpp"

#else

#define debug(...)

#endif



#define int long long // at least int64 > 9*10^18

#define EL '\n'

#define rep(i,n) for(int i = 0; i < (n); i++)

#define print(i) std::cout << (i) << '\n'

#define all(v) (v).begin(), (v).end()

using Graph = std::vector<std::vector<int> >;

const int INF = 1e17;

/* libraries */



constexpr int MOD = 1e9+7;

struct mint

{

	int v;

	mint():v(0){}

	mint(int v):v((v+MOD)%MOD){}

	mint operator-()const{ return mint(0) - *this; }

	mint& operator+=(const mint& a){ if((v+=a.v)>=MOD) v-=MOD; return *this; }

	mint& operator-=(const mint& a){ if((v+=MOD-a.v)>=MOD) v-=MOD; return *this; }

	mint& operator*=(const mint& a){ (v*=a.v)%=MOD; return *this; }

	mint& operator/=(const mint& a){ (*this) *= a.inv(); return *this; }

	mint operator+(const mint& a)const{ return mint(*this) += a; }

	mint operator-(const mint& a)const{ return mint(*this) -= a; }

	mint operator*(const mint& a)const{ return mint(*this) *= a; }

	mint operator/(const mint& a)const{ return mint(*this) /= a; }

	bool operator<(const mint& a)const{ return v < a.v; }

	bool operator==(const mint& a)const{ return v == a.v; }

	mint pow(int k)const{ mint r(1),t(v); while(k){ if(k&1) r*=t; t*=t; k>>=1; } return r; }

	mint inv()const{ return pow(MOD-2); }

	static mint comb(int n, int k) {

		if(n-k<k) k=n-k;

		mint num(1), dom(1);

		for(int i=0;i<k;i++) { num*=n-i; dom*=i+1; }

		return num/dom;

	}

};

std::istream& operator>>(std::istream&i,mint&a){ int t; i>>t; a=mint(t); return i; }

std::ostream& operator<<(std::ostream&o,const mint&a){ o<<a.v; return o; }





signed main() {

	int n,k;

	std::cin >> n >> k;

	mint ans=0;

	for(int i=k;i<=n+1;i++) {

		mint x = mint(2*n-i+1)*mint(i)/mint(2)-mint(i-1)*mint(i)/mint(2)+mint(1);

		ans+=x;

	}

	print(ans);

	return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef string str;

typedef vector<ll> vel;

typedef vector<str> ves;

#define REP(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)

#define rep(i, b) REP(i, 0, b)

const ll cd = 1000000007;

const double pi = 3.14159265358979;



int main() {

  	constexpr ll MOD=1e9+7;

    ll N, K;

    cin >> N >> K;

    ll count = 0;

    vel sum(N + 2);

    sum[0] = 0;

    rep(i, N+1) {

        sum[i+1] = sum[i] + i;

    }

    REP(i, K, N+2) {

        ll max, min;

        max = sum[N+1] - sum[N+1-i];

        min = sum[i] - sum[0];

        count += (max - min + 1)%MOD;

      	count%=MOD;

    }

    cout << count << endl;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <stdio.h>

#define m 1000000007

int main()

{

    int n,k;

    scanf("%d%d",&n,&k);

    unsigned long long a[200003]={},i,j,s=0;

    for(i=1;i<=n+1;i++) {

        a[i]=1+i*(n+1-i);

        a[i]=a[i]%m;

    }

    for(j=k;j<=n+1;j++) {

        s+=a[j];

    }

    printf("%llu\n",s%m);

    



    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define ll long long 

using namespace std;

ll M=(ll)1e9+7;



ll sum(ll n){

	return n*(n+1LL)/2LL;

}



int main(){

	// freopen("Input.txt","r",stdin); freopen("Output.txt","w",stdout);

	ll n,k,ans=0;cin>>n>>k;

	for(int i=k;i<=n;++i){

		ll l=sum(i-1);

		ll r=sum(n)-sum(n-i);

		ans+=(r-l+1);

	}

	ans++;

	ans=ans%M;

	cout<<ans<<"\n";

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define rep(i,N) for(int i=0;i<(N);i++)

#define FOR(i,a,b) for(int i=(a);i<(b);i++)

using namespace std;

const long long MOD = 1e9 + 7;

const long long INF = 1e12;

const int inf = 1e9;

typedef long long ll;

typedef pair<ll,int> P;

typedef set<int> S;

int main(){

    int n,k;

    cin>>n>>k;

    set<ll> sll;

    vector<ll> v(200010,0);

    rep(i,200010){

        v[i+1]=v[i]+i+1;

    }

    //ll left = MOD+1;

    //ll right = 0;

    ll ans = 0;

    for(int i=k;i<=n;i++){

        ll ans1=0,ans2=0;

        ans1 = v[i-1];

        ans2 = v[n]-v[n-i];

        /*cout<<i<<endl;

        cout<<v[i-1]<<" "<<v[n]<<" "<<v[n-i]<<endl;

        cout<<ans1<<" "<<ans2<<endl;*/

        ans=(ans+ans2-ans1+1)%MOD;

        //cout<<ans<<endl<<endl;

    }

    cout<<ans+1<<endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define rep(i,N) for(int i=0;i<(N);i++)

#define FOR(i,a,b) for(int i=(a);i<(b);i++)

using namespace std;

const long long MOD = 1e9 + 7;

const long long INF = 1e12;

const int inf = 1e9;

const int mod = 1e9+7;

typedef long long ll;

typedef pair<ll,int> P;

typedef set<int> S;

int main(){

	cout << fixed << setprecision(10);

	ll ans = 0;

	ll n,k;

	cin >> n >> k;

	for(ll i=k; i<=n+1;i++){

		ll left = i*(0+i-1)/2;

		ll right = i*(n+n-i+1)/2;

		ans += right - left + 1;

		ans %= mod;

	}

	cout << ans << endl;

	return 0;

}

// Fastest known solution for this problem: