Problem:
Score : 400 points

Problem Statement

We have N+1 integers: 10^{100}, 10^{100}+1, ..., 10^{100}+N.

We will choose K or more of these integers. Find the number of possible values of the sum of the chosen numbers, modulo (10^9+7).

Constraints

1 \leq N \leq 2\times 10^5

1 \leq K \leq N+1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible values of the sum, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

10

The sum can take 10 values, as follows:

(10^{100})+(10^{100}+1)=2\times 10^{100}+1

(10^{100})+(10^{100}+2)=2\times 10^{100}+2

(10^{100})+(10^{100}+3)=(10^{100}+1)+(10^{100}+2)=2\times 10^{100}+3

(10^{100}+1)+(10^{100}+3)=2\times 10^{100}+4

(10^{100}+2)+(10^{100}+3)=2\times 10^{100}+5

(10^{100})+(10^{100}+1)+(10^{100}+2)=3\times 10^{100}+3

(10^{100})+(10^{100}+1)+(10^{100}+3)=3\times 10^{100}+4

(10^{100})+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+5

(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+6

(10^{100})+(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=4\times 10^{100}+6

Sample Input 2

200000 200001

Sample Output 2

1

We must choose all of the integers, so the sum can take just 1 value.

Sample Input 3

141421 35623

Sample Output 3

220280457

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace abc163_d
{
  class Program
  {
    static void Main(string[] args)
    {
      var N = CIn.ReadInt();
      var K = CIn.ReadInt();

      var mod = new Mod((int)1e9 + 7);

      var ans = 0L;

      for (int i = K; i <= N + 1; i++)
      {
        ans = mod.Sum(ans, mod.Mul(N - i + 1, i), 1);
      }

      Console.WriteLine(ans);
    }
  }

  class Mod
  {
    public int N { get; }
    private readonly List<long> fac = new List<long>() { 1, 1 };
    private readonly List<long> inv = new List<long>() { 0, 1 };
    private readonly List<long> facInv = new List<long>() { 1, 1 };

    public Mod(int n) { N = n; }

    public long Add(long a, long b) => (a % N + b % N + N) % N;
    public long Sub(long a, long b) => Add(a, -b);
    public long Mul(long a, long b) => (a % N * b % N + N) % N;
    public long Div(long a, long b) => Mul(a, Inv(b));
    public long Sum(params long[] args) => args.Aggregate(Add);

    public long Inv(long a)
    {
      if (a < inv.Count) return inv[(int)a];
      long b = N, x = 1, y = 0, tmp;
      while (b != 0)
      {
        var q = a / b;
        tmp = y;
        y = x - q * y;
        x = tmp;
        tmp = b;
        b = a - q * b;
        a = tmp;
      }
      return (x % N + N) % N;
    }

    public long Pow(long a, long n)
    {
      var result = 1L;
      while (n > 0)
      {
        if ((n & 1) == 1) result = Mul(result, a);
        a = Mul(a, a);
        n >>= 1;
      }
      return result;
    }

    public void InitSeq(int n)
    {
      for (var i = fac.Count; i <= n; i++)
      {
        fac.Add(fac[i - 1] * i % N);
        inv.Add(N - inv[N % i] * (N / i) % N);
        facInv.Add(facInv[i - 1] * inv[i] % N);
      }
    }

    public long Fac(int n)
    {
      InitSeq(n);
      return fac[n];
    }

    public long Per(int n, int k)
    {
      if (n < k || n < 0 || k < 0) return 0;
      InitSeq(n);
      return fac[n] * facInv[n - k] % N;
    }

    public long Com(int n, int k)
    {
      if (n < k || n < 0 || k < 0) return 0;
      InitSeq(n);
      return fac[n] * (facInv[k] * facInv[n - k] % N) % N;
    }
  }

  static class CIn
  {
    private static readonly Queue<string> fieldBuffer = new Queue<string>();

    public static string Read()
    {
      if (fieldBuffer.Count == 0)
      {
        foreach (var field in Console.ReadLine().Split())
        {
          fieldBuffer.Enqueue(field);
        }
      }
      return fieldBuffer.Dequeue();
    }

    public static int ReadInt() => int.Parse(Read());
    public static long ReadLong() => long.Parse(Read());
    public static double ReadDouble() => double.Parse(Read());

    public static string[] ReadStringArray(int n)
    {
      var array = new string[n];
      for (var i = 0; i < n; i++)
      {
        array[i] = Read();
      }
      return array;
    }

    public static int[] ReadIntArray(int n)
    {
      var array = new int[n];
      for (var i = 0; i < n; i++)
      {
        array[i] = ReadInt();
      }
      return array;
    }

    public static long[] ReadLongArray(int n)
    {
      var array = new long[n];
      for (var i = 0; i < n; i++)
      {
        array[i] = ReadLong();
      }
      return array;
    }

    public static double[] ReadDoubleArray(int n)
    {
      var array = new double[n];
      for (var i = 0; i < n; i++)
      {
        array[i] = ReadDouble();
      }
      return array;
    }

    public static char[,] ReadCharMatrix(int h, int w)
    {
      var array = new char[h, w];
      for (var i = 0; i < h; i++)
      {
        var l = Console.ReadLine().ToCharArray();
        for (var j = 0; j < w; j++)
        {
          array[i, j] = l[j];
        }
      }
      return array;
    }
  }
}