n,m=map(int,input().split())
p = (int)(1e9+7) 

def power(x, y, p) : 
    res = 1    
    x = x % p  
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
  
        y = y >> 1      
        x = (x * x) % p 
    return res 

def f(a, b, mod): 
  
    res = 0; # Initialize result 
  
    # Update a if it is more than 
    # or equal to mod 
    a = a % mod; 
  
    while (b): 
      
        # If b is odd, add a with result 
        if (b & 1): 
            res = (res + a) % mod; 
              
        # Here we assume that doing 2*a 
        # doesn't cause overflow 
        a = (2 * a) % mod; 
  
        b >>= 1; # b = b / 2 
      
    return res; 

if n>m:
    n,m = m,n
ans = 0
if n==1 and m==1:
    ans = 0
elif n==1:
    ans = m-1
elif m==1:
    ans = n-1
else:
    ans = f(power(n,n,p),(m-n+1),p)

print(ans)

