Problem:
Score : 500 points

Problem Statement

Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following conditions:

A_i \neq B_i, for every i such that 1\leq i\leq N.

A_i \neq A_j and B_i \neq B_j, for every (i, j) such that 1\leq i < j\leq N.

Since the count can be enormous, print it modulo (10^9+7).

Constraints

1\leq N \leq M \leq 5\times10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the count modulo (10^9+7).

Sample Input 1

2 2

Sample Output 1

2

A_1=1,A_2=2,B_1=2,B_2=1 and A_1=2,A_2=1,B_1=1,B_2=2 satisfy the conditions.

Sample Input 2

2 3

Sample Output 2

18

Sample Input 3

141421 356237

Sample Output 3

881613484

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

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

namespace AtCoder.Abc172
{
    class ModularFactorial
    {
        long mod;
        long[] factorial;
        long[] inverse;

        public ModularFactorial(int maxN, int maxK, long mod)
        {
            this.mod = mod;

            factorial = new long[maxN + 1];
            factorial[0] = 1;
            for (int i = 1; i <= maxN; i++)
                factorial[i] = factorial[i - 1] * i % mod;

            int maxInvN = Math.Max(maxN - maxK, maxK);
            inverse = new long[maxInvN + 1];
            inverse[0] = 1;
            for (int i = 1; i <= maxInvN; i++)
                inverse[i] = Inverse(factorial[i]);
        }

        public long GetCombination(int n, int k)
            => (factorial[n] * inverse[k]) % mod * inverse[n - k] % mod;

        public long GetPermutation(int n, int k)
            => factorial[n] * inverse[n - k] % mod;

        public long GetPermutation(int n)
            => factorial[n];

        public long Inverse(long a)
        {
            while (a < 0)
                a += mod;
            a %= mod;
            long p = mod;
            long ya = 0;
            long yp = 1;
            while (a > 0)
            {
                long n = p / a;
                long r = p % a;
                long yr = ya - n * yp;
                ya = yp;
                yp = yr;
                p = a;
                a = r;
            }
            return ya < 0 ? ya + mod : ya % mod;
        }
    }

    class E
    {
        static long mod = 1000000007;

        public static void Main()
        {
            var nm = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            var n = nm[0];
            var m = nm[1];
            var factorial = new ModularFactorial(m, n, mod);

            var mPn = factorial.GetPermutation(m, n);

            long ans = mPn;
            bool keven= false;
            for (int k = 1; k <= n; k++)
            {
                var diff = factorial.GetPermutation(m - k, n - k) * factorial.GetCombination(n, k) % mod;
                ans = keven ? (ans + diff) % mod : (ans - diff) % mod;
                keven = !keven;
            }
            ans = ans * mPn % mod;
            Console.WriteLine(ans < 0 ? ans + mod : ans);
        }
    }
}