Scanf.scanf "%d %d" (fun n m ->
    let extgcd x y =
        let rec eg r0 r1 a0 a1 b0 b1 =
            if r1 = 0 then a0 else
                let q1 = r0 / r1 in
                eg r1 (r0 mod r1) a1 (a0 - q1 * a1) b1 (b0 - q1 * b1)
        in
        eg x y 1 0 0 1
    in
    let inv x y = (* 1/x in mod y *)
        let c = extgcd x y in
        if c < 0 then c + y else c
    in
    let z = 1_000_000_000 + 7 in
    let ( +@) a b = (((a + b) mod z) + z) mod z  in
    let ( *@) a b = ((a mod z) * (b mod z)) mod z in
    let ( /@) a b = a *@ inv b z in
    let fact = Array.make 500001 1 in

    for i = 1 to 500000 do
        fact.(i) <- fact.(i - 1) *@ i
    done;

    let combin n r = fact.(n) /@ fact.(n - r) /@ fact.(r) in
    let permut n r = fact.(n) /@ fact.(n - r) in

    let rec loop k flag acc =
        if k > n then acc else
            let g = permut (m - k) (n - k) in
            loop (k + 1) (-flag)
                (acc +@ flag *@ combin n k *@ permut m k *@ g *@ g)
    in
    let ans = loop 0 1 0 in
    Printf.printf "%d\n" ans
)