#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdint>

template <std::int64_t Modulus>
class ModularInteger {
 public:
  static constexpr std::int64_t kModulus = Modulus;

  constexpr ModularInteger() : value_(0) {}
  constexpr ModularInteger(std::int64_t value) : value_(value % kModulus) {
    value_ += (value_ < 0) * kModulus;
  }
  constexpr std::int64_t GetValue() const {
    return value_;
  }

  constexpr ModularInteger operator+=(ModularInteger other) {
    value_ += other.value_ - (value_ + other.value_ >= kModulus) * kModulus;
    return *this;
  }
  constexpr ModularInteger operator-=(ModularInteger other) {
    value_ += (value_ < other.value_) * kModulus - other.value_;
    return *this;
  }
  constexpr ModularInteger operator*=(ModularInteger other) {
    value_ = value_ * other.value_ % kModulus;
    return *this;
  }
  constexpr ModularInteger operator/=(ModularInteger other) {
    value_ = value_ * other.Inverse().value_ % kModulus;
    return *this;
  }

  constexpr ModularInteger operator+(ModularInteger other) const {
    return ModularInteger(*this) += other;
  }
  constexpr ModularInteger operator-(ModularInteger other) const {
    return ModularInteger(*this) -= other;
  }
  constexpr ModularInteger operator*(ModularInteger other) const {
    return ModularInteger(*this) *= other;
  }
  constexpr ModularInteger operator/(ModularInteger other) const {
    return ModularInteger(*this) /= other;
  }

    constexpr ModularInteger Inverse() const {
    auto a = kModulus;
    auto b = value_;
    std::int64_t x = 0;
    std::int64_t y = 1;
    while (b > 0) {
      const auto q = a / b;
      a -= q * b;
      x -= q * y;
      std::swap(a, b);
      std::swap(x, y);
    }
    return x + (x < 0) * kModulus;
  }

  constexpr ModularInteger Power(std::int64_t exponent) const {
    if (value_ == 0) {
      return 0;
    }
    if (exponent < 0) {
      return Inverse().Power(-exponent);
    }
    ModularInteger product = 1;
    auto multiplier = *this;
    while (exponent > 0) {
      if (exponent & 1) {
        product *= multiplier;
      }
      multiplier *= multiplier;
      exponent >>= 1;
    }
    return product;
  }

  friend constexpr std::ostream& operator<<(std::ostream& stream, ModularInteger i) {
    return stream << i.value_;
  }
  friend constexpr std::istream& operator>>(std::istream& stream, ModularInteger i) {
    return stream >> i.value_;
  }

 private:
  std::int64_t value_;
};

template <std::int64_t Modulus>
class CombinationCalculator {
 public:
  static constexpr std::int64_t kModulus = Modulus;

  CombinationCalculator(int max_population) :
      factorials_(max_population + 1), inverse_factorials_(max_population + 1) {
    factorials_[0] = 1;
    for (int i = 1; i <= max_population; ++i) {
      factorials_[i] = factorials_[i - 1] * i;
    }
    inverse_factorials_.back() = factorials_.back().Inverse();
    for (int i = max_population; i > 0; --i) {
      inverse_factorials_[i - 1] = inverse_factorials_[i] * i;
    }
  }

  ModularInteger<kModulus> Permutation(int n, int k) {
    return factorials_[n] * inverse_factorials_[n - k];
  }

  ModularInteger<kModulus> Combination(int n, int k) {
    return factorials_[n] * inverse_factorials_[k] * inverse_factorials_[n - k];
  }

 private:
  std::vector<ModularInteger<kModulus>> factorials_;
  std::vector<ModularInteger<kModulus>> inverse_factorials_;
};

int main() {
  constexpr std::int64_t kModulus = 1000000007;
  int N, M;
  std::cin >> N >> M;

  CombinationCalculator<kModulus> calculator(M);
  auto sum = calculator.Permutation(M, N).Power(2);
  for (int i = 1; i <= N; ++i) {
    const auto count = calculator.Permutation(M, N) * calculator.Combination(N, i) * calculator.Permutation(M - i, N - i);
    if (i & 1) {
      sum -= count;
    } else {
      sum += count;
    }
  }

  std::cout << sum<< std::endl;
  return 0;
}


Measured memory_bytes: 11008