Problem:


Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477



Sample input:
2 2


Sample output:
8

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
from _collections import defaultdict
from functools import lru_cache

class Scanner():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def map_int():
        return [int(x) for x in Scanner.string().split()]
    def string_list(n):
        return [input() for i in range(n)]
    def int_list_list(n):
        return [Scanner.mapInt() for i in range(n)]
    def int_cols_list(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3,d + 1,2):
            if n % i == 0:
                return False
        return True

MOD = int(1e09) + 7

def main():
    N,M = Scanner.map_int()
    if not abs(N - M) <= 1:
        print(0)
        return
    
    n ,m = 1,1
    for i in range(2,N + 1):
        n *= i
        n%=MOD
    for i in range(2,M + 1):
        m *= i
        m %= MOD
    ans = 0
    if N == M:
        ans = (n * m) % MOD * 2 % MOD
    else:
        ans = n * m % MOD
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from functools import reduce,lru_cache
import math
from operator import mul

def main():
  #sys.setrecursionlimit(10 ** 5 + 1)
  N,M = map(int,input().split())
  
  @lru_cache(maxsize = None)
  def recur(n):
    if n == 0:
      return 1
    else:
      m = n * recur(n - 1)
      return m
      
  if N == M:
    L = [i for i in range(1,N + 1)]
    #ans = (reduce(mul,L) ** 2) * 2  % (10 ** 9 + 7)
    ans = (math.factorial(M) ** 2) * 2 % ((10 ** 9) + 7)
    print(ans)
  elif abs(M - N) == 1: 
    ans = math.factorial(N) * math.factorial(M) % ((10 ** 9) + 7)
    print(ans)
    
  else:
    print(0)
    
main()

Submission status: Accepted

Solution code:
N,M = map(int, input().split())

mod = 10**9+7

x = max(N, M)
y = min(N, M)
if x-y <= 1:
    ans = 1
    for i in range(1, x+1):
        ans *= i
        ans %= mod
    for i in range(1, y+1):
        ans *= i
        ans %= mod
    if x == y:
        ans *= 2
        ans %= mod
else:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
import math
n, m = map(int, input().split())
mod = 10**9+7

fact_n = math.factorial(n)
fact_m = math.factorial(m)

if n == m:
    print((fact_n*fact_m*2)%mod)
elif abs(n - m) == 1:
    print(fact_n*fact_m%mod)
else:
    print(0)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
MOD = 1000000007
 
def fac(n):
    ans = 1
    for i in range(1, n + 1):
        ans = (ans * i) % MOD
    return ans
 
if abs(m - n) > 1:
    print(0)
elif m == n:
    print((2 * fac(m) * fac(n)) % MOD)
else:
    print((fac(m) * fac(n)) % MOD)

Submission status: Accepted

Solution code:
#第三問　reconciled
mod=10**9+7
n,m=map(int,input().split())
def find_power(n):
    # 0!からn!までのびっくりを出してくれる関数
    powlist=[0]*(n+1)
    powlist[0]=1
    powlist[1]=1
    for i in range(2,n+1):
        powlist[i]=powlist[i-1]*i%(10**9+7)
    return powlist
p=find_power(10**5+4)
if abs(n-m)>1:
    print(0)
elif n==m:
    print(p[n]*p[m]*2%mod)
else:
    print(p[n]*p[m]%mod)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
if abs(N-M) > 1:
    print(0)
    exit()
MOD = 10**9 + 7
m = 1
n = 1
for i in range(1, M+1):
    m *= i
    m %= MOD
for i in range(1, N+1):
    n *= i
    n %= MOD

if M == N:
    print(m*n*2 % MOD)
else:
    print(m*n % MOD)

Submission status: Accepted

Solution code:
N, M = map(int, input().split(' '))
import math
n, m = int(math.factorial(N)), int(math.factorial(M))
l = int(math.pow(10,9)) + 7

if N == M:
    n *= m * 2
    n %= l
    print(n)

elif N + 1 == M:
    n *= m
    n %= l
    print(n)

elif N == M + 1:
    n *= m
    n %= l
    print(n)

else:
    print(0)

Submission status: Accepted

Solution code:
import math
n,m = map(int,input().split())
num = 1
if n==m:
  for i in range(1,n+1):
    num *= i
    num = num%(10**9+7)
  print((num*num*2)%(10**9+7))
elif abs(n-m)==1:
  print(int(math.factorial(n)*math.factorial(m)%(10**9+7)))
else:
  print(0)

Submission status: Accepted

Solution code:
from math import factorial as fc

n, m = map(int, input().split())
if abs(n-m) == 1:
  print(fc(n)*fc(m)%(10**9+7))
elif abs(n-m) == 0:
  print(fc(n)*fc(m)*2%(10**9+7))
else:
  print(0)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
import math
if abs(n-m)>1:
  print(0)
  exit(0)
mod=10**9+7
ans=1
k=math.factorial(n)%mod
l=math.factorial(m)%mod
if n==m:
  ans=2
print(ans*(k*l)%mod)

Submission status: Accepted

Solution code:
from math import factorial

N, M = map(int, input().split())
ans = factorial(N)*factorial(M)
if abs(N-M) == 1:
    print(ans % (10**9+7))
elif N == M:
    print((ans*2) % (10**9+7))
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())

ex = [1]
mod = 10**9+7
for i in range(1,10**5+1):
    ex.append((ex[-1]*i)%mod)
    
if abs(N-M) >= 2:
    print(0)
elif N==M:
    print((2*ex[N]**2)%mod)
else:
    print((ex[N]*ex[M])%mod)

Submission status: Accepted

Solution code:
import math

n, m = map(int, input().split())
mod = 10 ** 9 + 7

if abs(n-m) > 1:
    print(0)
else:
    ans = 1

    for i in range(1, n+1):
        ans = ans * i % mod
    for j in range(1, m+1):
        ans = ans * j % mod

    if abs(n-m) == 0:
        print(2 * ans % mod)
    else:
        print(ans % mod)

Submission status: Accepted

Solution code:
import math

n, m = map(int, input().split())
mod = 10 ** 9 + 7

if abs(n-m) > 1:
    print(0)
    exit()

if abs(n-m) == 0:
    ans = math.factorial(n) * math.factorial(m) * 2 % mod
    print(ans)
else:
    ans = math.factorial(n) % mod * math.factorial(m) % mod
    print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
if abs(n - m) < 2:
    cnt = 1
    for i in range(1, n + 1):
        cnt = (cnt * i) % (10 ** 9 + 7)
    for i in range(1, m + 1):
        cnt = (cnt * i) % (10 ** 9 + 7)
    print((cnt * 2) % (10 ** 9 + 7) if m == n else cnt)
else:
    print(0)

Submission status: Accepted

Solution code:
mod=10**9+7


def main():
    n, m = map(int, input().split())
    if abs(n - m) > 1:
        return 0


    fact = 1
    for i in range(1, min(n, m) + 1):
        fact = fact * i % mod



    if n == m:
        return fact ** 2 * 2 % mod
    else:
        return fact * fact * (min(n, m) + 1) % mod




print(main())

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
ppp = 10**9 + 7

def fac(n):
    r = [1]*(n+1)
    for i in range(1, n+1):
        r[i] = r[i-1] * i % ppp
    return r[n]

if abs(N-M) > 1:
    print(0)
elif N - M == 0:
    print(fac(N)**2*2%ppp)
else:
    print(fac(N)*fac(M)%ppp)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
MOD = 1000000007

def fac(n):
    ans = 1
    for i in range(1, n + 1):
        ans = (ans * i) % MOD
    return ans

if abs(m - n) > 1:
    print(0)
elif m == n:
    print((2 * fac(m) * fac(n)) % MOD)
else:
    print((fac(m) * fac(n)) % MOD)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#ARC076C
import sys
import math

#n=int(input())

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

n=hoge[0]
m=hoge[1]

#total=1
#if(n==m):
#	total+=1

t = math.factorial(min(n,m))
#print(t)
#print(t**2)
if(abs(n-m)>1):
	print(0)
elif(n==m):
	print((2*(t**2))%1000000007)
else:
	print(((t**2)*max(n,m))%1000000007)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
if abs(a - b) >= 2:
    print(0)
else:
    c = 1
    for i in range(1, a + 1):
        c = (c * i) % (10 ** 9 + 7)
    for i in range(1, b + 1):
        c = (c * i) % (10 ** 9 + 7)
    if abs(a - b) == 1:
        print(c)
    else:
        print((c * 2) % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
N, M = list(map(int,input().split()))
if abs(N - M) >= 2:
    print(0)
else:
    ans = 1
    if N == M:
        ans = 2
    for i in range(1, N+1):
        ans = (ans * i) % ((10 ** 9) + 7)
    for i in range(1, M+1):
        ans = (ans * i) % ((10 ** 9) + 7)
    print(ans)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
c = 1
if abs(a-b) >= 2:
    print(0)
elif abs(a-b) == 1:
    for i in range(1, a+1):
        c = (c * i) % (10**9+7)
    for i in range(1, b+1):
        c = (c * i) % (10**9+7)
    print(c % ((10**9)+7))
elif abs(a-b) == 0:
    for i in range(1, a+1):
        c = (c * i * i) % ((10**9)+7)
    print((c*2) % ((10**9)+7))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
if n>m:
	n,m=m,n
if n==m:
	ans=2
elif n==m-1:
	ans=1
else:
	ans=0
mod=10**9+7
if ans!=0:
	for i in range(1,n+1):
		ans=(ans*i)%mod
	for i in range(1,m+1):
		ans=(ans*i)%mod
	print(ans)
else:
	print(0)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import math

N, M = map(int, input().split())
if abs(N - M) >= 2:
    print(0)
elif abs(N - M) == 1:
    print(math.factorial(N) * math.factorial(M) % (pow(10, 9) + 7))
else:
    print(math.factorial(N) * math.factorial(M) * 2 % (pow(10, 9) + 7))

Submission status: Accepted

Solution code:
def factorial(n):
    ans = 1
    for i in range(2, n + 1):
        ans = ans * i % 1000000007
    return ans

N,M = map(int, input().split())
mod = 10**9+7

if abs(N-M) > 1:
    print(0)
    exit()
p = factorial(min(N,M))
if N == M:
    print((p**2)*2%mod)
else:
    print(p**2*max(N,M)%mod)

Submission status: Accepted

Solution code:
# ARC076C - Reconciled?
from math import factorial


def main():
    n, m = list(map(int, input().rstrip().split()))
    MOD = 10 ** 9 + 7
    n, m = min(n, m), max(n, m)
    dif = m - n
    if dif > 1:  # must be ...DDM...
        print(0)
    else:
        if dif == 0:  # start with either D or M (DM..., MD...)
            ans = (factorial(n) ** 2 * 2) % MOD  # n == m -> (n!)^2 * 2
        else:  # sandwich (DMD or MDM)
            ans = (factorial(n) ** 2 * m) % MOD  # n + 1 == m -> (n!)^2 * m
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC076C - Reconciled?
def factorial_mod(x):
    MOD = 10 ** 9 + 7
    ret = 1
    for i in range(1, x + 1):
        ret = (ret * i) % MOD
    return ret


def main():
    n, m = list(map(int, input().rstrip().split()))
    MOD = 10 ** 9 + 7
    n, m = min(n, m), max(n, m)
    dif = m - n
    if dif > 1:  # must be ...DDM...
        print(0)
    else:
        if dif == 0:  # start with either D or M (DM..., MD...)
            ans = (factorial_mod(n) ** 2 * 2) % MOD
        else:  # sandwich (DMD or MDM)
            ans = (factorial_mod(n) ** 2 * m) % MOD
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC076C - Reconciled?
def main():
    n, m = list(map(int, input().rstrip().split()))
    MOD = 10 ** 9 + 7
    n, m = min(n, m), max(n, m)
    dif = m - n
    if dif > 1:  # must be ...DDM...
        print(0)
    else:
        prod = 1
        for i in range(1, n + 1):  # the factorial of n
            prod = (prod * i) % MOD

        if dif == 0:  # start with either D or M (DM..., MD...)
            ans = (prod ** 2 * 2) % MOD
        else:  # sandwich (DMD or MDM)
            ans = (prod ** 2 * m) % MOD
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC076C - Reconciled?
from math import factorial


def main():
    n, m = list(map(int, input().rstrip().split()))
    MOD = 10 ** 9 + 7
    dif = abs(n - m)
    if abs(n - m) not in (0, 1):
        print(0)
    else:
        if dif == 0:
            ans = (factorial(n) * factorial(m) * 2) % MOD
        else:
            ans = (factorial(n) * factorial(m)) % MOD
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class Combinatorics:
    def __init__(self, N, mod):
        '''
        Preprocess for calculating binomial coefficients nCr (0 <= r <= n, 0 <= n <= N)
        over the finite field Z/(mod)Z.
        Input:
            N (int): maximum n
            mod (int): a prime number. The order of the field Z/(mod)Z over which nCr is calculated.
        '''
        self.mod = mod
        self.fact = {i: None for i in range(N+1)}         # n!
        self.inverse = {i: None for i in range(1, N+1)}   # inverse of n in the field Z/(MOD)Z
        self.fact_inverse = {i: None for i in range(N+1)} # inverse of n! in the field Z/(MOD)Z
        
        # preprocess
        self.fact[0] = self.fact[1] = 1
        self.fact_inverse[0] = self.fact_inverse[1] = 1
        self.inverse[1] = 1
        for i in range(2, N+1):
            self.fact[i] = i * self.fact[i-1] % self.mod
            q, r = divmod(self.mod, i)
            self.inverse[i] = (- (q % self.mod) * self.inverse[r]) % self.mod
            self.fact_inverse[i] = self.inverse[i] * self.fact_inverse[i-1] % self.mod
    
    def perm(self, n, r):
        '''
        Calculate nPr = n! / (n-r)! % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return (self.fact[n] * self.fact_inverse[n-r]) % self.mod
    
    def binom(self, n, r):
        '''
        Calculate nCr = n! /(r! (n-r)!) % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return self.fact[n] * (self.fact_inverse[r] * self.fact_inverse[n-r] % self.mod) % self.mod
        
    def hom(self, n, r):
        '''
        Calculate nHr = {n+r-1}Cr % mod.
        Assign r objects to one of n classes.
        Arrangement of r circles and n-1 partitions:
            o o o | o o | | | o | | | o o | | o
        '''
        if n == 0 and r > 0:
            return 0
        if n >= 0 and r == 0:
            return 1
        return self.binom(n + r - 1, r)

N, M = map(int, input().split())
MOD = 10**9 + 7
if abs(N-M) > 1:
    print(0)
else:
    com = Combinatorics(max(N, M), MOD)
    if N != M:
        ans = (com.fact[N] * com.fact[M]) % MOD
    else:
        ans = (2 * (com.fact[N] * com.fact[M]) % MOD) % MOD
    print(ans)

Submission status: Accepted

Solution code:
import math

n,m = map(int, input().split())


ans = 0
if abs(n-m) <= 1:
  if n == m:
    ans = (math.factorial(n) * math.factorial(m) * 2) % (10**9 + 7)
  else:
    ans = (math.factorial(n) * math.factorial(m)) % (10**9 + 7)
    
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
MOD = 10 ** 9 + 7
if N > M:
    N, M = M, N
if abs(N - M) >= 2:
    print(0)
else:
    MOD = 10 ** 9 + 7
    P = 100005
    fact = [0] * P
    fact[0] = 1
    for i in range(1, P):
        fact[i] = fact[i - 1] * i
        fact[i] %= MOD

    if N == M:
        print(fact[N] * fact[M] * 2 % MOD)
    elif N + 1 == M:
        print(fact[N] * fact[M] % MOD)

Submission status: Accepted

Solution code:
import math

N, M = map(int, input().split())

if N == M:
    print((2 * math.factorial(N) * math.factorial(M)) % (10 ** 9 + 7))
elif N == M + 1 or N + 1 == M:
    print((math.factorial(N) * math.factorial(M))% (10 ** 9 + 7))
else:
    print(0)

Submission status: Accepted

Solution code:
import math
N,M=map(int,input().split(' '))
if abs(N-M) not in (0,1):
  print(0)
else:
  x = math.factorial(N) % (10**9+7)
  y = math.factorial(M) % (10**9+7)
  print(((2 - abs(N-M))*x*y)%(10**9+7))

Submission status: Accepted

Solution code:
import math
n,m=map(int,input().split())
if abs(n-m)>1:print(0);exit()
ans=(math.factorial(n)*math.factorial(m))%(10**9+7)
if n==m:ans*=2;ans%=10**9+7
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())

mod = 10**9+7 #出力の制限
P = 10**5
g1 = [1, 1] # 元テーブル 階乗

for i in range( 2, P + 1 ):
    g1.append( ( g1[-1] * i ) % mod )

if abs(N - M) == 0:
    print(g1[N] * g1[M] * 2 % mod)
elif abs(N - M) == 1:
    print(g1[N] * g1[M] % mod)
else:
    print(0)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())

if N==M:
  ans = 1
  for i in range(1,N+1):
    ans *= i
    ans %= 10**9 + 7
  ans *= ans
  ans %= 10**9 + 7
  ans *= 2
  ans %= 10**9 + 7
elif N==M+1 or N+1==M:
  ans = 1
  for i in range(1,N+1):
    ans *= i
    ans %= 10**9 + 7
  for i in range(1,M+1):
    ans *= i
    ans %= 10**9 + 7
else:
  ans = 0
print(ans)

Submission status: Accepted

Solution code:
def factorial(n):
  ans = 1
  for i in range(n):
    ans *= i+1
    ans %= mod
  return ans

n,m = map(int, input().split())
mod = 10**9 + 7
diff = abs(n-m)
if diff == 0:
  print((((factorial(n)**2)%mod)*2)%mod)
elif diff == 1:
  print((factorial(n)*factorial(m))%mod)
else:
  print(0)

Submission status: Accepted

Solution code:
MAX_REMAINDER = 10 ** 9 + 7
N, M = map(int, raw_input().split())
if abs(N - M) >= 2:
  print 0
elif abs(N - M) == 1:
  print reduce(lambda x, y: x * y % MAX_REMAINDER, (i + 1 for i in range(N))) \
    * reduce(lambda x, y: x * y % MAX_REMAINDER, (i + 1 for i in range(M))) \
    % MAX_REMAINDER
else:
  print (reduce(lambda x, y: x * y % MAX_REMAINDER, (i + 1 for i in range(N))) \
    * reduce(lambda x, y: x * y % MAX_REMAINDER, (i + 1 for i in range(M)))) \
    * 2 \
    % MAX_REMAINDER

Submission status: Accepted

Solution code:
N,M=map(int,input().split())


ans=1 if(abs(N-M)<2) else 0
n=max(N,M)

fa=0
for i in range(1,n+1):
    fa=ans
    ans=ans*i%(10**9+7)
    
if(N==M):
    ans=(ans*ans*2)%(10**9+7)
else:
    ans=((ans)*fa)%(10**9+7)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 10 10:05:08 2019

@author: shinjisu
"""


# ARC 076 C
import math


def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def dmp(x):
    global debug
    if debug:
        print(x)


def probC():
    N, M = getIntList()
    N1097 = 10**9 + 7
    dmp((N, M))
    if N == M:
        times = 2
    elif abs(N-M) == 1:
        times = 1
    else:
        times = 0
    dmp(times)
    # if times > 0:
    count = math.factorial(N) * math.factorial(M) * times % N1097
    return count


debug = False
print(probC())

Submission status: Accepted

Solution code:
import math
INF, MOD = float("inf"), 10 ** 9 + 7
MAX, MIN = -INF, INF
dx1, dy1, dx2, dy2 = [-1, 0, 1, 0], [0, -1, 0, 1], [-1, 0, 1, -1, 1, -1, 0, 1], [-1, -1, -1, 0, 0, 1, 1, 1]

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

def mins(x, y):
    x = min(x, y)

def maxs(x, y):
    x = max(x, y)

def fact(n):
    tmp = 1
    for i in range(2, n + 1):
        tmp = int(tmp * i % MOD)
    return tmp

while(True):
    try:
        n, m = get_int_list()
        if(n == m):
            print(int(fact(n) ** 2 * 2 % MOD))
        elif(abs(n - m) == 1):
            print(int(fact(n) * fact(m) % MOD))
        else:
            print(0)
    except EOFError:
        exit()

Submission status: Accepted

Solution code:
class FactMod():
    '''
    modの値が素数の時のfactと組み合わせを求める
    フェルマーの小定理を用いているため、modが素数以外の時は使えない
    '''

    def __init__(self, n, mod):
        '''
        コンストラクタ
        f:nまでの i!の値を　配列に入れる
        inv: (i!)^-1　の値を配列に入れる
        '''
        self.f = [1]*(n+1)
        for i in range(1, n+1):
            self.f[i] = self.f[i-1]*i % mod

        self.inv = [pow(self.f[-1], mod-2, mod)]
        for i in range(1, n+1)[::-1]:
            self.inv.append(self.inv[-1]*i % mod)
        self.inv.reverse()

    def fact(self, n):
        '''
        n!の値を返す
        '''
        return self.f[n]


N, M = map(int, input().split())

mod = 10**9+7
ans = 0
F = FactMod(max(N, M), mod)

if abs(N-M) == 1:
    ans = F.fact(N)*F.fact(M) % mod
elif N == M:
    ans = F.fact(N)*F.fact(N)*2 % mod
else:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
MOD = int(1e9) + 7

n, m = map(int, input().split())

if abs(m - n) >= 2:
  ans = 0
else:
  ans = 1
  for i in range(1, m+1):
    ans *= i
    ans %= MOD
  for i in range(1, n+1):
    ans *= i
    ans %= MOD
  if n == m:
    ans = (ans * 2) % MOD

print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = 1
b = 1
if abs(n-m) >= 2:
    print(0)
else:
    for i in range(2, n+1):
        a = (a*i) % (10**9+7)
    for j in range(2, m+1):
        b = (b*j) % (10**9+7)
    if n == m:
        print((a*b*2) % (10**9+7))
    else:
        print((a*b) % (10**9+7))

Submission status: Accepted

Solution code:
a,b = map(int,input().split(" "))
c = 1
d = 1
for i in range(2,a+1):
    c = (c * i) % (10 ** 9 + 7)
for i in range(2,b+1):
    d = (d * i) % (10 ** 9 + 7)
if abs(a - b) >= 2:
    print(0)
elif a == b:
    print(((c * d) * 2) % (10 ** 9 + 7))
else:
    print((c * d) % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
N, M = map(int, input().split(' '))
MOD = 10**9 + 7
if abs(N-M) > 1:
    print(0)
    exit()

ans = 1
for i in range(N):
    ans = ans * (i+1) % MOD
for i in range(M):
    ans = ans * (i+1) % MOD
if N == M:
    ans = ans * 2 % MOD
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())

if abs(N - M) >= 2:
    print(0)

elif N == M:
    ans = 1
    for i in range(1,N+1):
        ans = (ans * i) % (10 ** 9 + 7)
    for i in range(1, M + 1):
        ans = (ans * i) % (10 ** 9 + 7)
    print((2 * ans) % (10 ** 9 + 7))
else:
    ans = 1
    for i in range(1,N+1):
        ans = (ans * i) % (10 ** 9 + 7)
    for i in range(1, M + 1):
        ans = (ans * i) % (10 ** 9 + 7)
    print(ans % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
mod = 1000000007
n,m = map(int, input().split())
ans = 1 if abs(n-m) == 1 else 2
if abs(n-m) > 1:
    print(0)
    quit()
for i in range(1,n+1):
    ans *= i
    ans %= mod
for i in range(1,m+1):
    ans *= i
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
N, M = inpl()
mod = 10**9+7
fac = [1 for _ in range(N+3)]
for i in range(N+2):
    fac[i+1] = (i+1)*fac[i]%mod

if abs(N-M) > 1:
    print(0)
elif N == M:
    print(2*fac[N]*fac[M]%mod)
else:
    print(fac[N]*fac[M]%mod)

Submission status: Accepted

Solution code:
import math
n,m = list(map(int, input().split()))
n, m = max(n,m), min(n, m)

if n - m > 1:
    ans = 0
elif n - m == 1:
    k = math.factorial(m) % (10**9 + 7)
    ans = k*k*n % (10**9 + 7)
else:
    k = math.factorial(m) % (10**9 + 7)
    ans = k*k*2 % (10**9 + 7)
print(ans)

Submission status: Accepted

Solution code:
import math

N, M = map(int, input().split())
ans=0
inf=(10**9)+7
if abs(N-M)==0:
    ans=2*math.factorial(N)*math.factorial(M)
elif abs(N-M)==1:
    ans=math.factorial(N)*math.factorial(M)
else:
    ans=0
print(ans%inf)

Submission status: Accepted

Solution code:
import math
N,M = map(int,input().split())
if abs(N - M) > 1:
  print(0)
elif N == M:
  print((2*(math.factorial(N) * math.factorial(M))) % 1000000007)
else:
  print(((math.factorial(N) * math.factorial(M))) % 1000000007)

Submission status: Accepted

Solution code:
import sys
import collections
import math
from collections import Counter
from collections import deque

N, M = [int(x) for x in input().split()]

D = 10 ** 9 + 7

if abs(N - M) >= 2:
    print(0)
    sys.exit(0)

ret = math.factorial(N) * math.factorial(M)

if N == M:
    ret *= 2


print(ret % D)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

def kaijo(n):
    k = 1
    for i in range(1, n + 1):
        k = k * i % MOD
    return k

n, m = map(int, input().split())
if n == m:
    print((kaijo(n) ** 2 * 2) % MOD)
elif abs(n - m) > 1:
    print(0)
else:
    print((kaijo(n) * kaijo(m)) % MOD)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

def kaijo(n):
    k = 1
    for i in range(1, n + 1):
        k = k * i % MOD
    return k

n, m = map(int, input().split())
if n == m:
    print((kaijo(n) ** 2 * 2) % MOD)
elif abs(n - m) > 1:
    print(0)
else:
    if n < m:
        print((kaijo(n) ** 2 * m) % MOD)
    else:
        print((kaijo(m) ** 2 * n) % MOD)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
else:
    if N < M:
        N, M = M, N
    ans = 1
    for i in range(1, M + 1):
        ans = ans * i % mod
    if N == M:
        print(ans ** 2 * 2 % mod)
    else:
        print(ans ** 2 * N % mod)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
if abs(n - m) >= 2:
  print("0")
else:
  if n == m:cou = 2
  else:cou = 1
  while n > 1:
    cou *= n
    cou %= (10 ** 9 + 7)
    n -= 1
  while m > 1:
    cou *= m
    cou %= (10 ** 9 + 7)
    m -= 1
  print(cou)

Submission status: Accepted

Solution code:
import sys
N,M=map(int,input().split())
if abs(N-M)>=2:
    print(0)
    sys.exit()
if N<M:
    N,M=M,N#Nが大きい
table=[1]*(N+1)
mod=10**9+7
for i in range(1,N+1):
    table[i]=(table[i-1]*i)%mod
if N!=M:
    ans=(table[N]*table[M])%mod
    print(ans)
    sys.exit()
print((2*table[N]*table[M])%mod)

Submission status: Accepted

Solution code:
# cook your dish here
n,m = map(int,input().split())
c = [1]
#c[i]:i!
for i in range(1,10**5+10):
    w = (c[-1]*i)%(10**9+7)
    c.append(w)
#print(c)
if m ==n-1:
    print((c[m]*c[n])%(10**9+7))
elif m == n:
    print((c[m]*c[n]*2)%(10**9+7))
elif m == n+1:
    print((c[m]*c[n])%(10**9+7))
else:
    print(0)

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *
from heapq import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

MAXN = 10**6
MOD = 10**9 + 7

fact = [1] * MAXN
for i in range(1, MAXN):
  fact[i] = (fact[i-1] * i) % MOD

N, M = reads()
if abs(N-M) >= 2:
  print(0); exit()

ans = fact[N] * fact[M] * (2 if N == M else 1) % MOD
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int,input().split())
MOD = 1000000007

def f(n):
    ans = 1
    for i in range(1,n+1):
        ans*=i
        ans%=MOD
    return ans

sum = 0
if -2<n-m<2:
    if n<m:
        n,m = m,n
    sum = f(n)*f(m)*2**(m-n+1)
    sum %= MOD
print(sum)

Submission status: Accepted

Solution code:
import math
n, m = map(int,input().split())

sum = 0
if -2<n-m<2:
    if n<m:
        n,m = m,n
    sum = math.factorial(n)*math.factorial(m)*2**(m-n+1)
    sum %= 1000000007
print(sum)

Submission status: Accepted

Solution code:
mod=1000000007
fact=[1 for i in range(100001)]
for i in range(1,100001):
 fact[i]=(fact[i-1]*i)%mod
s=input().split()
n=int(s[0])
m=int(s[1])
n,m=sorted((n,m))
if n==m-1:
 print((fact[n]*fact[m])%mod)
elif n==m:
 print((fact[n]*fact[m]*2)%mod)
else:
 print(0)

Submission status: Accepted

Solution code:
import math

n,m = list(map(int,input().split()))

if abs(n-m) >= 2:
    print(0)
elif n == m:
    print(2*math.factorial(n)**2 % 1000000007)
else:
    print(math.factorial(n)*math.factorial(m)%1000000007)

Submission status: Accepted

Solution code:
import math
N,M=map(int,input().split())
reg=10**9+7

#犬と猿が隣合わない並び方*Nの階乗*Mの階乗(並べ方を順列で表現）
#入力例1
#犬と猿の数が同じ場合
#犬猿犬猿,猿犬猿犬の２通り*2の階乗*2の階乗
#どちらかが１匹多い場合
#入力例2
#犬猿犬猿犬の1通り*3の階乗*2の階乗

#ここでどちらか一方の数が他方より２以上多い場合を考えると題を満たす並べ方はない

if abs(N-M)>=2:
  print(0)
  exit()
fact=((math.factorial(N)%reg)*(math.factorial(M)%reg))%reg
if abs(N-M)==1:
  print(fact)
else:
  print((2*fact)%reg)

Submission status: Accepted

Solution code:
n, m = list(map(int, input().split()))

MOD = 10**9+7
MAX = max(n, m)+2
fact = [0]*(MAX)
fact[0] = 1
for i in range(1, MAX):
  fact[i] = (fact[i-1]*i)%MOD

if abs(n-m)>1:
  ans = 0
else:
  if n==m:
    ans = fact[n]*fact[m]*2%MOD
  else:
    ans = fact[n]*fact[m]%MOD
print(ans)

Submission status: Accepted

Solution code:
import math
N, M = map(int, input().split())
if abs(M-N) >= 2:
    print(0)
elif M == N:
    ans = math.factorial(M) * math.factorial(N) * 2 % (10**9+7)
    print(ans)
else:
    ans = math.factorial(M) * math.factorial(N) % (10**9+7)
    print(ans)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
Mod=10**9+7

if abs(a-b)>1:
  print(0)
elif abs(a-b)==1:
  ans=max(a,b)
  for i in range(2,max(a,b)):
    ans*=(i*i)
    ans%=Mod
  print(ans)
else:
  ans=2
  for i in range(2,a+1):
    ans*=(i*i)
    ans%=Mod
  print(ans)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
n, m = map(int, input().split())

if abs(n - m) >= 2:
	print(0)

else:
	n, m = (max(n, m), min(n, m))
	ans = 1
	for i in range(1, m + 1):
		ans = (ans * i) % MOD

	if n == m:
		print((ans ** 2 * 2) % MOD)
	else:
		print((ans ** 2 * n) % MOD)

Submission status: Accepted

Solution code:
mod = 1000000007

N,M = map(int,input().split())

if abs(N-M)>1:
    print(0)
else:
    ans = 1
    for i in range(1,N+1): ans = (ans*i)%mod
    for j in range(1,M+1): ans = (ans*j)%mod
    if N==M: ans = (ans*2)%mod
    print(ans)

Submission status: Accepted

Solution code:
l = map(int, raw_input().split())
n = l[0]
m = l[1]
mod = 1000000000 + 7

'''
def mod_pow(x,n,mod): 
    ll ret = 1;
    while n > 0:
        if n & 1 :
            (ret *= x) %= mod
        (x *= x) %= mod
        n /= 2
    return ret

def combo(n,k,mod):
    if n / 2 < k:
        k = n - k
    bunshi = 1
    for i in range(n - k + 1,n + 1):
        bunshi *= i;
        bunshi %= mod;
    bunbo = 1
    for i in range(1,k + 1):
        bunbo *= i;
        bunbo *= mod;

    (bunshi *= mod_pow(bunbo(bunshi,mod - 2,mod)) %= mod)
'''

def f(n):
    ret = 1
    for i in  range(1,n + 1) :
        ret *= i
        ret %= mod
    return ret

if m < n :
    l = n
    n = m
    m = l

if n == m :
    print (2 * f(n) * f(n) % mod)
elif m - n < 2 :
    print (f(n) * f(m) % mod)
else:
    print (0)