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.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using static System.Math;

namespace CSharpSpiral {
  class Program {
    public static void Main(string[] args) {
      var sw = new StreamWriter(Console.OpenStandardOutput()) {
        AutoFlush=
#if DEBUG
      true
#else
			false
#endif
      };
      Console.SetOut(sw);
      new Program().Solve(new ConsoleInput(
#if DEBUG
      new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
      , ' '));
      Console.Out.Flush();
    }
    public void Solve(ConsoleInput cin) {
      int mod = 1_000_000_007;
      var N = cin.ReadInt;
      var M = cin.ReadInt;
      var combi = new long[N+1];
      combi[0]=1;
			var F = new ResidueField(mod);
			for(int i = 0; i<N; i++) {
				combi[i+1]=F.Calculate(combi[i], N-i, i+1, '/', '*');
			}
			F.Factorial(M);
      var S = Enumerable.Range(0, N+1).Select(
        s=> ((s&1)==0 ? 1 : -1)*F.Calculate(combi[s],M,s,'P',M-s,N-s,'P',2,'^','*','*')
      ).Sum()%mod;
			S=(S<0 ? (S+mod) : S)%mod;
			Console.WriteLine(S);
    }
  }

  static class ExtendClass {
    public static string Join<T>(this IEnumerable<T> X, string separator = " ") => string.Join(separator, X);
    public static int LastOrDefault(this SortedSet<int> S) {
      return S.Any() ? S.Last() : int.MaxValue;
    }
    public static void MergeSort<T>(this T[] A, bool desending) where T : IComparable<T> {
      if(desending)
        MergeSort(A, Comparer<T>.Create((x, y) => y.CompareTo(x)));
      else
        MergeSort(A, Comparer<T>.Create((x, y) => x.CompareTo(y)));
    }
    public static void MergeSort<T>(T[] A, IComparer<T> comparer) where T : IComparable<T> {
      MergeSort1(0, A.Length, A, comparer);
    }
    private static void Merge<T>(int left, int mid, int right, T[] A, IComparer<T> comparer)
      where T : IComparable<T> {
      var nl = mid-left;
      var nr = right-mid;
      var L = new T[nl];
      var R = new T[nr];
      for(int i = 0; i<nl; i++)
        L[i]=A[i+left];
      for(int i = 0; i<nr; i++)
        R[i]=A[i+mid];
      int il = 0, ir = 0;
      for(int k = left; k<right; k++) {
        if(comparer.Compare(L[il], R[ir])<=0) {
          A[k]=L[il++];
          if(il==nl) {
            k++;
            for(; ir<nr;)
              A[k++]=R[ir++];
            break;
          }
        } else {
          A[k]=R[ir++];
          if(ir==nr) {
            k++;
            for(; il<nl;)
              A[k++]=L[il++];
            break;
          }
        }
      }
    }
    private static void MergeSort1<T>(int left, int right, T[] A, IComparer<T> comparer)
    where T : IComparable<T> {
      if(left+1<right) {
        var mid = (left+right)/2;
        MergeSort1(left, mid, A, comparer);
        MergeSort1(mid, right, A, comparer);
        Merge(left, mid, right, A, comparer);
      }
    }
  }
  class ConsoleInput {
    private readonly TextReader _stream;
    private readonly char _separator = ' ';
    private readonly Queue<string> inputStream;
    public ConsoleInput(TextReader stream, char separator = ' ') {
      this._separator=separator;
      this._stream=stream;
      inputStream=new Queue<string>();
    }
    public string Read {
      get {
        if(inputStream.Count!=0) return inputStream.Dequeue();
        string[] tmp = _stream.ReadLine().Split(_separator);
        for(int i = 0; i<tmp.Length; ++i)
          inputStream.Enqueue(tmp[i]);
        return inputStream.Dequeue();
      }
    }
    public string ReadLine { get { return _stream.ReadLine(); } }
    public int ReadInt { get { return int.Parse(Read); } }
    public long ReadLong { get { return long.Parse(Read); } }
    public double ReadDouble { get { return double.Parse(Read); } }
    public string[] ReadStrArray(long N) {
      var ret = new string[N];
      for(long i = 0; i<N; ++i)
        ret[i]=Read;
      return ret;
    }
    public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i<N; ++i) ret[i]=ReadInt; return ret; }
    public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i<N; ++i) ret[i]=ReadLong; return ret; }
    public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i<N; ++i) ret[i]=ReadDouble; return ret; }
    public bool isEOF {
      get { return _stream.Peek()==-1; }
    }
	}
	class ResidueField: QuotientRing {
		public ResidueField(int _modulo = 1000*1000*1000+7) : base(_modulo) { }
		private static long extgcd(long a, long b, ref long x, ref long y) {
			if(b==0) {
				x=1; y=0; return a;
			}
			var q = a/b;
			var g = extgcd(b, a-q*b, ref x, ref y);
			var z = x-q*y;
			x=y; y=z;
			return g;
		}
		public long Inv(long a) {
			if(a==0) throw new DivideByZeroException();
			long x = 0, y = 0;
			extgcd(a, modulo, ref x, ref y);
			return mod(x);
		}
		public long Div(long a, long b) => Mul(a, Inv(b));
		public override long Permutation(int n, int k) {
			if(n>1000000)return base.Permutation(n, k);
			return Div(Factorial(n), Factorial(n-k));
		}
		public long Combination(int n, int k) {
			if(k<0||k>n) return 0;
			if(n-k<k) k=n-k;
			return Div(Permutation(n, k), Factorial(k));
		}
		public override long Calculate(params object[] RPN) {
			var S = new Stack<long>();
			foreach(var token in RPN) {
				var type = token.GetType();
				if(type==typeof(int)) {
					S.Push((int)token);
				} else if(type==typeof(long)) {
					S.Push((long)token);
				} else if(type==typeof(char)) {
					long v1 = 0L, v2 = 0L;
					v2=S.Pop();
					HashSet<char> unaries = new HashSet<char>() { '!' };
					if(!unaries.Contains((char)token))
						v1=S.Pop();
					long res;
					switch((char)token) {
						case '+': res=Add(v1, v2); break;
						case '-': res=Sub(v1, v2); break;
						case '*': res=Mul(v1, v2); break;
						case '/': res=Div(v1, v2); break;
						case '^': res=Pow(v1, v2); break;
						case '!': res=Factorial((int)v2); break;
						case 'C': res=Combination((int)v1, (int)v2); break;
						case 'P': res=Permutation((int)v1, (int)v2); break;
						default: throw new InvalidOperationException();
					}
					S.Push(res);
				} else throw new InvalidOperationException();
			}
			return S.Peek();
		}
	}
	class QuotientRing {
		public readonly long modulo;
		public QuotientRing(int mModulo = 1000*1000*1000+7) {
			modulo=mModulo;
		}
		protected long[] factorialTable = { 1 };
		private int factorialTableLastIndex { get { return factorialTable.Length-1; } }
		public long mod(long x) => x<0 ? x%modulo+modulo : x%modulo;
		public long Add(long a, long b) => mod(a+b);
		public long Sub(long a, long b) => mod(a-b);
		public long Mul(long a, long b) => mod(a*b);
		public long Pow(long a, long n) {
			if(n==0) return 1;
			var R = 1L;
			n=n<0 ? n%(modulo-1)+(modulo-1) : n%(modulo-1);
			for(; n>0; a=Mul(a, a), n>>=1) {
				if((n&1)!=0) R=Mul(R, a);
			}
			return R;
		}
		private void ExpandFactorialTable(int N) {
			var k = factorialTableLastIndex;
			Array.Resize(ref factorialTable, N+1);
			for(int i = k+1; i<=N; i++) {
				if(i==0) { factorialTable[i]=1; continue; }
				factorialTable[i]=Mul(factorialTable[i-1], i);
			}
		}
		public long Factorial(int n) {
			if(n>factorialTableLastIndex)
				ExpandFactorialTable(n);
			return factorialTable[n];
		}
		public virtual long Permutation(int n, int k) {
			if(k<0||k>n) return 0;
			var Res = 1L;
			for(int i = 0; i<k; i++)
				Res=Mul(Res, n-i);
			return Res;
		}
		public virtual long Calculate(params object[] RPN) {
			var S = new Stack<long>();
			foreach(var token in RPN) {
				var type = token.GetType();
				if(type==typeof(int)||type==typeof(long)) {
					S.Push((long)token);
				} else if(type==typeof(char)) {
					long v1 = 0L, v2 = 0L;
					v2=S.Pop();
					HashSet<char> unaries = new HashSet<char>() { '!' };
					if(!unaries.Contains((char)token))
						v1=S.Pop();
					long res;
					switch((char)token) {
						case '+': res=Add(v1, v2); break;
						case '-': res=Sub(v1, v2); break;
						case '*': res=Mul(v1, v2); break;
						case '^': res=Pow(v1, v2); break;
						case '!': res=Factorial((int)v2); break;
						default: throw new InvalidOperationException();
					}
					S.Push(res);
				} else throw new InvalidOperationException();
			}
			return S.Peek();
		}
	}
}