Ancient Egyptians are known to have used a large set of symbols $\sum$ to write on the walls of the temples. Fafa and Fifa went to one of the temples and found two non-empty words S_1 and S_2 of equal lengths on the wall of temple written one below the other. Since this temple is very ancient, some symbols from the words were erased. The symbols in the set $\sum$ have equal probability for being in the position of any erased symbol.

Fifa challenged Fafa to calculate the probability that S_1 is lexicographically greater than S_2. Can you help Fafa with this task?

You know that $|\sum|= m$, i. e. there were m distinct characters in Egyptians' alphabet, in this problem these characters are denoted by integers from 1 to m in alphabet order. A word x is lexicographically greater than a word y of the same length, if the words are same up to some position, and then the word x has a larger character, than the word y.

We can prove that the probability equals to some fraction $P / Q$, where P and Q are coprime integers, and $Q \neq 0 \text{mod}(10^{9} + 7)$. Print as the answer the value $R = P \cdot Q^{-1} \operatorname{mod}(10^{9} + 7)$, i. e. such a non-negative integer less than 10^9 + 7, such that $R \cdot Q \equiv P \operatorname{mod}(10^{9} + 7)$, where $a \equiv b \text{mod}(m)$ means that a and b give the same remainders when divided by m.


-----Input-----

The first line contains two integers n and m (1 ≤ n,  m ≤ 10^5) — the length of each of the two words and the size of the alphabet $\sum$, respectively.

The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ m) — the symbols of S_1. If a_{i} = 0, then the symbol at position i was erased.

The third line contains n integers representing S_2 with the same format as S_1.


-----Output-----

Print the value $P \cdot Q^{-1} \operatorname{mod}(10^{9} + 7)$, where P and Q are coprime and $P / Q$ is the answer to the problem.


-----Examples-----
Input
1 2
0
1

Output
500000004

Input
1 2
1
0

Output
0

Input
7 26
0 15 12 9 13 0 14
11 1 0 13 15 12 0

Output
230769233



-----Note-----

In the first sample, the first word can be converted into (1) or (2). The second option is the only one that will make it lexicographically larger than the second word. So, the answer to the problem will be $\frac{1}{2} \operatorname{mod}(10^{9} + 7)$, that is 500000004, because $(500000004 \cdot 2) \operatorname{mod}(10^{9} + 7) = 1$.

In the second example, there is no replacement for the zero in the second word that will make the first one lexicographically larger. So, the answer to the problem is $\frac{0}{1} \operatorname{mod}(10^{9} + 7)$, that is 0.

Difficulty: interview

Starter code:

Solutions:
n, m = [int(x) for x in input().split()]

a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

mult = 1
mod = 10 ** 9 + 7
res = 0

m_inv = pow(m, mod - 2, mod)

for x, y in zip(a, b):
    if x and y:
        if x > y:
            res += mult
            res %= mod
            break
        elif x == y:
            continue
        else:
            break
    elif x:
        res += mult * (x-1) * m_inv % mod
        res %= mod
        mult = mult * m_inv % mod
    elif y:
        res += mult * (m - y) * m_inv % mod
        res %= mod
        mult = mult * m_inv % mod
    else:
        res += mult * m * (m - 1) // 2 * m_inv * m_inv % mod
        res %= mod
        mult = mult * m_inv % mod
print(res)


import sys, math

#f = open('input/input_2', 'r')
f = sys.stdin

def modInverse(a, m) :
    g = gcd(a, m)
    if (g != 1) :
        return None
    else:
        return  power(a, m - 2, m)

# To compute x^y under modulo m
def power(x, y, m) :
    if (y == 0) :
        return 1
    p = power(x, y // 2, m) ** 2
    if (y & 1 == 1):
      p *= x
    return (p % m)

# Function to return gcd of a and b
def gcd(a, b) :
    while (a != 0):
      t = b % a
      b = a
      a = t
    return b

n, m = list(map(int, f.readline().split()))
s1 = list(map(int, f.readline().split()))
s2 = list(map(int, f.readline().split()))

#0 same, 1 big
pos = [1, 0]
all_pos = 1
for a1, a2 in zip(s1, s2):
  next_pos = [0, 0]
  if a1 == 0 and a2 == 0:
    next_pos[0] += pos[0] * m
    next_pos[1] += pos[0] * m * (m-1) // 2
    next_pos[1] += pos[1] * m * m

    all_pos *= m*m
  elif a1 == 0:
    next_pos[0] += pos[0]
    next_pos[1] += pos[0] * (m - a2)
    next_pos[1] += pos[1] * m

    all_pos *= m
  elif a2 == 0:
    next_pos[0] += pos[0]
    next_pos[1] += pos[0] * (a1 - 1)
    next_pos[1] += pos[1] * m

    all_pos *= m
  else:
    if a1 > a2:
      next_pos[1] += pos[0]
    elif a1 == a2:
      next_pos[0] += pos[0]
    next_pos[1] += pos[1]
  pos = next_pos
  if pos[0] == 0:
    break
  g = gcd(pos[0], all_pos)
  g = gcd(pos[1], g)
  pos[0] //= g
  pos[1] //= g
  all_pos //= g
  pos[0] %= 1000000007
  pos[1] %= 1000000007
  all_pos %= 1000000007

p = pos[1]
q = all_pos
pq_gcd = gcd(p,q)
p //= pq_gcd
q //= pq_gcd

q_inv = modInverse(q, 1000000007)
print( (p * q_inv) % 1000000007)


def rev(a, m):
    res = 1
    k = m-2
    s = a
    while k > 0:
        if k%2 == 1:
            res = (res * s) % m
        k //= 2
        s = (s**2) % m
    return res

n, m = [int(x) for x in input().split()]
a1 = [int(x) for x in input().split()]
a2 = [int(x) for x in input().split()]
a = []
mul = 1
for x,y in zip(a1, a2):
    if x == 0 and y == 0:
        count = (m*(m-1))//2
        counteq = m
        maxcount = m**2
    elif x == 0:
        count = m - y
        counteq = 1
        maxcount = m
    elif y == 0:
        count = x-1
        counteq = 1
        maxcount = m
    else:
        count = 1 if x>y else 0
        counteq = 1 if x==y else 0
        maxcount = 1

    a.append((count, counteq, maxcount))
    if counteq == 0: break
a.reverse()

##print(a)

P, Q = 0, 1
for c, ce, mc in a:
    P = (P*ce+ Q*c) % (10**9+7)
    Q = (Q*mc)  % (10**9+7)
  

res, zeros = P, Q
print((res * rev(zeros, 10**9+7)) % (10**9+7))



p=10**9+7

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

a=input().split()
b=input().split()

a=[int(k) for k in a]
b=[int(k) for k in b]

invm=pow(m,p-2,p)
inv2=pow(2,p-2,p)

ans=0

for i in reversed(range(n)):
	if a[i]==0 and b[i]==0:
		ans=(((inv2*invm)%p)*(m-1)+invm*ans)%p
	if a[i]==0 and b[i]!=0:
		ans= ((m-b[i])*invm+invm*ans)%p
	if a[i]!=0 and b[i]==0:
		ans= ((a[i]-1)*invm+invm*ans)%p
	if a[i]!=0 and b[i]!=0:
		if a[i]>b[i]:
			ans= 1
		elif b[i]>a[i]:
			ans= 0
		else:
			ans= ans

print(ans)

n, m = [int(x) for x in input().split()]
s1 = [int(x) for x in input().split()]
s2 = [int(x) for x in input().split()]
mod = int(1e9 + 7)

e = 1
mi = pow(m, mod - 2, mod)
mi2 = pow(2 * m, mod - 2, mod)
res = 0

for i in range(n):
    if s1[i] == 0 and s2[i] != 0:
        res += ((((m - s2[i]) * mi) % mod) * e) %mod
        e = (e * mi) % mod
    elif s1[i] != 0 and s2[i] == 0:
        res += ((((s1[i] - 1) * mi) % mod) * e) %mod
        e = (e * mi) % mod
    elif s1[i] == 0 and s2[i] == 0:
        res += ((((m - 1)* mi2) % mod) * e)%mod
        e = (e * mi) % mod
    else:
        if s1[i] > s2[i]:
            res += e
            break
        if s1[i] < s2[i]:
            break
    res = res % mod
print(res%mod)


MOD = 10**9 + 7
n,m = [int(x) for x in input().split()]
s1 = [int(x) for x in input().split()]
s2 = [int(x) for x in input().split()]

mi = pow(m, MOD - 2, MOD)
m2 = pow(2, MOD - 2, MOD)
ans = 0
mul = 1
for a,b in zip(s1, s2):
	if a == 0 and b == 0:
		ans = (ans + mul * ((m - 1)*(mi * m2))) % MOD
		mul = (mul * mi) % MOD
	elif a == 0:
		ans = (ans + (mul * (m - b) * mi)) % MOD
		mul = (mul * mi) % MOD
	elif b == 0:
		ans = (ans + mul * (a - 1) * mi) % MOD
		mul = (mul * mi) % MOD
	elif a > b:
		ans = (ans + mul * 1) %  MOD
		break
	elif a == b:
		pass
	elif a < b:
		break
print(ans % MOD)

def fast(a,b):
    if(b==0):
        return 1 
    if(b%2==1):
        return a*fast(a,b-1)%mod 
    x=fast(a,b/2)%mod
    return x*x%mod
n,m=map(int,input().split())
a=list(map(int,(input().split())))
b=list(map(int,(input().split())))
p=0
q=1
z1=1
z2=1
mod=1000000007
for i in range(0,n):
    if(a[i]!=0 and b[i]!=0):
        if(a[i]==b[i]):continue
        if(a[i]>b[i]):
            p=(p*z2%mod+q*z1%mod)%mod   
            q=(q*z2)%mod
        break
    if(a[i]==0 and b[i]!=0):
        p=((p*m*z2)%mod+(z1*q*(m-b[i]))%mod)%mod
        q=(q*z2*m)%mod
        z2=(z2*m)%mod
    if(a[i]!=0 and b[i]==0):
        p=((p*m*z2)%mod+(z1*q*(a[i]-1))%mod)%mod
        q=(q*z2*m)%mod
        z2=(z2*m)%mod
    if(a[i]==0 and b[i]==0):
        p=(p*m*2*z2+z1*(q)*(m-1))%mod
        q=(q*z2*2*m)%mod
        z2=(z2*m)%mod
print(p*(fast(q,(mod-2))%mod)%mod)

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

MOD = 10**9 + 7
p, q = 0, 1
for i in reversed(list(range(n))):
	if s1[i] == 0 and s2[i] == 0:
		p = (q * (m - 1) + 2 * p) % MOD
		q = (2 * m * q) % MOD
	elif s1[i] == 0:
		p = (q * (m - s2[i]) + p) % MOD
		q = (m * q) % MOD
	elif s2[i] == 0:
		p = (q * (s1[i] - 1) + p) % MOD
		q = (m * q) % MOD
	elif s1[i] < s2[i]:
			p, q = 0, 1
	elif s1[i] > s2[i]:
			p, q = 1, 1

def mpow(a, n):
	if n == 0:
		return 1
	t = mpow(a, n // 2)
	t = (t * t) % MOD
	if n % 2 == 1:
		t = (t * a) % MOD
	return t

inv_q = mpow(q, MOD - 2)
r = (p * inv_q) % MOD

print(r)


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

MOD = 10 ** 9 + 7
invm = pow(m, MOD - 2, MOD)
inv2 = pow(2, MOD - 2, MOD)

z = 0
p = 1

for a, b in zip(s1, s2):
    if a == 0 and b == 0:
        z = (z + p * ((m - 1) * (invm * inv2))) % MOD
        p = (p * invm) % MOD
    elif a == 0:
        z = (z + (p * (m - b) * invm)) % MOD
        p = (p * invm) % MOD
    elif b == 0:
        z = (z + (p * (a - 1) * invm)) % MOD
        p = (p * invm) % MOD
    elif a > b:
        z = (z + p) % MOD
        break
    elif a < b:
        break
print(z)


from math import gcd

p = 10 ** 9 + 7


class RN:
    p = 10 ** 9 + 7

    def __init__(self, u, d):
        self.u = u
        self.d = d

    def __str__(self):
        return str(self.u) + '/' + str(self.d)

    def cleanup(self):
        self.u = self.u % p
        self.d = self.d % p


    def add(self, u, d):
        self.u = self.u * d + u * self.d
        self.d = self.d * d
        self.cleanup()


    def mult(self, u, d):

        return (self.u * u)%p, (self.d * d)%p

    def mult_ip(self, u, d):
        # r.cleanup()
        self.u, self.d = self.u * u, (self.d * d) % p


    def get_m_exp(self):
        self.cleanup()
        a = gcd(self.u, self.d)
        self.u //= a
        self.d //= a

        # print(str(self))
        return ((pow(self.d, p - 2, p)) * self.u) % p


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


def sum_ch(n):
    return ((n + 1) * n) // 2


def sol(s1, s2, m):
    strgr = RN(0, 1)

    eq = RN(1, 1)
    for gr, sm, i in zip(s1, s2, list(range(len(s1)))):

        strgr.cleanup()


        if gr != 0 and sm != 0:

            if gr == sm:
                continue
            elif gr < sm:
                return strgr.get_m_exp()
            else:
                strgr.add(eq.u, eq.d)
                return strgr.get_m_exp()
        else:
            if gr == sm == 0:
                strgr.add(*eq.mult(sum_ch(m - 1), m * m))

            elif gr == 0:
                strgr.add(*eq.mult(m - sm, m))

            else:
                strgr.add(*eq.mult(gr - 1, m))
            eq.mult_ip(1, m)
    return strgr.get_m_exp()


'''
a = sol([0,0],[1,1], 2) # 3/4

a = sol([0,0,0],[1,1,1], 2) # 7/8
a = sol([0,1,1],[1,1,1], 2) # 1/2
a = sol([1,0,1],[1,1,1], 2) # 1/2

a = sol([0,0,0],[1,3,3], 3) # 2/2
a = sol([0,0,0],[1,1,0], 2) # 5/8
a = sol([0,0,0],[0,0,0], 2) # 13/16
'''

print(sol(s1, s2, m))


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

N = 10 ** 9 + 7
res = True
inv = pow(m, N - 2, N)
cur = 1
result = 0
inv_2 = pow(2, N - 2, N)

for i in range(n):
	if s1[i] == 0 and s2[i] == 0:
		result = (result + cur * inv_2  * (N + 1 - inv)) % N
		cur = (cur * inv) % N 
		continue

	if s1[i] == 0:
		cur = (cur * inv) % N 
		result = (result + (m - s2[i]) * cur) % N
		continue

	if s2[i] == 0:
		cur = (cur * inv) % N
		result = (result + (s1[i] - 1) * cur) % N
		continue

	if s1[i] < s2[i]:
		break

	if s1[i] > s2[i]:
		result = (result + cur) % N
		break

print(result % N)


MOD = 1000000007

n, m = map(int, input().split())
l1 = [int(x) for x in input().split()]
l2 = [int(x) for x in input().split()]

probLeft = 1 # probability that I'm continuing

def modInv(x, n = MOD - 2):
    if n <= 1:
        return (x ** n) % MOD
    tmp = (modInv(x, n // 2) ** 2) % MOD
    if n % 2 == 1:
        tmp = tmp * x % MOD
    return tmp

dp = {}

def fraction(x,y):
    if (x,y) in dp:
        return dp[(x,y)]
    dp[(x,y)] = x * modInv(y) % MOD
    return dp[(x,y)]

minv = fraction(1, m)

ans = 0
for i in range(n):
    a = l1[i]
    b = l2[i]
    if a == 0 or b == 0:
        if b > 0:
            ans += probLeft * (m - b) * minv % MOD
        elif a > 0:
            ans += probLeft * (a - 1) * minv % MOD
        else:
            ans += probLeft * ((m - 1) * m // 2) * minv * minv % MOD
        probLeft = probLeft * minv % MOD
    elif a > b:
        ans += probLeft
        break
    elif a == b:
        continue
    else:
        break

print(ans % MOD)

def f():
    nonlocal MOD, ans, factor
    ans %= MOD
    factor %= MOD

MOD = 10**9 + 7

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

reverse = pow(m, MOD - 2, MOD)
reverse_for_2 = (MOD + 1) // 2
ans = 0
factor = 1
for a, b in zip(a, b):
    if (a == b == 0):
        ans += factor * ((m - 1) * (reverse * reverse_for_2))
        factor *= reverse
        f()
    elif (a == 0):
        ans += factor * (m - b) * reverse
        factor *= reverse
        f()
    elif (b == 0):
        ans += factor * (a - 1) * reverse
        factor *= reverse
        f()
    elif (a > b):
        ans += factor
        f()
        break
    elif (a == b):
        pass
    elif (a < b):
        break
f()
print(ans)

rd = lambda: list(map(int, input().split()))
M = 1000000007
inv = lambda x: (M - M // x) * inv(M % x) % M if x - 1 else 1
n, m = rd()
a = list(rd())
b = list(rd())
i = 0
p, q, r = 0, 1, 1
while i < n:
    x, y = a[i], b[i]
    if x == 0 and y == 0:
        p = (p * m * m + r * ((m - 1) * m >> 1)) % M
        q = q * m * m % M
        r = r * m % M
    elif x == 0:
        p = (p * m + r * (m - y)) % M
        q = q * m % M
    elif y == 0:
        p = (p * m + r * (x - 1)) % M
        q = q * m % M
    else:
        if x == y:
            pass
        else:
            if x > y:
                p = (p + r) % M
            break
    i += 1
print(p * inv(q) % M)


rd = lambda: list(map(int, input().split()))
M = 1000000007
inv = lambda x: (M - M // x) * inv(M % x) % M if x - 1 else 1
n, m = rd()
a = list(rd())
b = list(rd())
p, q, r = 0, 1, 1
for i in range(n):
    x, y = a[i], b[i]
    if x == 0 and y == 0:
        p = (p * m * m + r * ((m - 1) * m >> 1)) % M
        q = q * m * m % M
        r = r * m % M
    elif x == 0:
        p = (p * m + r * (m - y)) % M
        q = q * m % M
    elif y == 0:
        p = (p * m + r * (x - 1)) % M
        q = q * m % M
    elif x != y:
        if x > y:
            p = (p + r) % M
        break
print(p * inv(q) % M)


from fractions import gcd;

def add(a,b,c,d):
	lcm =(b*d)//(gcd(b,d));
	aa = lcm//b;
	bb = lcm//d;
	#print("aa:",aa,"bb:",bb,"a:",a,"b:",b);
	aa = aa*a;
	bb = bb*c;
	#print("aa:",aa,"bb:",bb);
	cc = aa+bb;
	dd = gcd(lcm,cc);
	#print("cc:",cc);
	#print("lcm:",lcm);
	lcm = lcm//dd;
	cc = cc //dd;
	#print("cc:",cc);
	#print("lcm:",lcm);
	return cc,lcm;

def mul(a,b,c,d):
	aa = a*c;
	bb = b*d;
	dd = gcd(aa,bb);
	aa = aa//dd;
	bb = bb//dd;

	return aa,bb;

def eeuclid(a,b):
	q = a//b;
	r = a % b;

	if r == 1:
		return 1,-q;

	a,b = eeuclid(b,r);
	m = 1;
	n = -q;
	m = m * b;
	n = n * b;
	n = n + a;


	return m,n;

def mod_inverse(val,m):
	a,b = eeuclid(val,m);
	return a % m;

n,m = input().split();
n = int(n);
m = int(m);
mod_val = 1000000007;
arr1 = [];
arr2 = [];

arr1 = [int(x) for x in input().split()];
arr2 = [int(x) for x in input().split()];


num = [];
den = [];
prob = [];

for i in range(0,n):
	num.append(0);
	den.append(0);
	prob.append(0);
num.append(0);
den.append(1);
prob.append(0);

for i in range(n-1,-1,-1):
	if(arr1[i] != 0 and arr2[i] != 0):
		if(arr1[i] > arr2[i]):
			prob[i] = 1;
		elif(arr1[i] == arr2[i]):
			if(prob[i+1] == 0):
				prob[i] = 0;
			else:
				prob[i] = prob[i+1];
		elif(arr1[i] < arr2[i]):
			prob[i] = 0;
	
	elif(arr1[i] == 0 and arr2[i] != 0):
		num1 = m-arr2[i];
		inv1 = mod_inverse(m,mod_val);
		inv1 = inv1 % mod_val;
		qq = ((num1 % mod_val) * (inv1 % mod_val)) % mod_val;

		pp = inv1 * prob[i+1];

		prob[i] = (qq + pp) % mod_val;

	elif(arr1[i] != 0 and arr2[i] == 0):
		num1 = arr1[i]-1;
		inv1 = mod_inverse(m,mod_val);

		qq = ((num1 % mod_val) * (inv1 % mod_val)) % mod_val;

		pp = inv1 * prob[i+1];
		prob[i] = (qq + pp) % mod_val;
	else:
		aa = (m * (m-1))//2;
		aa = aa % mod_val;

		inv2 = mod_inverse(m*m,mod_val);
		inv1 = mod_inverse(m,mod_val);

		qq = (aa * inv2) % mod_val;
		pp = (inv1 * prob[i+1]) % mod_val;

		prob[i] = (pp + qq) % mod_val;

print((prob[0]));






n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
MOD = int(1e9 + 7)
m2 = pow(2, MOD - 2, MOD)
mm = pow(m, MOD - 2, MOD)
f = 1
ans = 0
for i, j in zip(a, b):
    if i == 0 and j == 0:
        ans += (m - 1) * m2 * mm * f
        ans %= MOD
        f = f * mm % MOD
    elif i == 0:
        ans += (m - j) * f * mm
        ans %= MOD
        f = f * mm % MOD
    elif j == 0:
        ans += (i - 1) * f * mm
        ans %= MOD
        f = f * mm % MOD
    elif i > j:
        ans += f
        ans %= MOD
        break
    elif i < j:
        break

print(ans)

# a and m must be coprime!
# returns x such that xa = 1 mod m
def modinverse(a, m):
    m0 = m
    y = 0
    x = 1

    if m == 1:
        return 0

    while a > 1:
        q = a // m
        t = m

        m = a % m
        a = t
        t = y

        y = x - q * y
        x = t

    if x < 0:
        x += m0
    
    return x
# runs in log(m)

M = (10 ** 9) + 7

line = [int(x) for x in input().split()]

n = line[0]
m = line[1]

Mi = modinverse(m, M)
Ti = modinverse(2, M)

a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

Pkp1 = 0
Pk = 0

if a[-1] == 0 and b[-1] == 0:
    Pkp1 = ((m-1) % M) * Mi * Ti
elif a[-1] == 0:
    Pkp1 = ((m-b[-1]) % M) * Mi
elif b[-1] == 0:
    Pkp1 = ((a[-1]-1) % M) * Mi
else:
    if a[-1] > b[-1]:
        Pkp1 = 1
    else:
        Pkp1 = 0

Pk = Pkp1

for i in range(1, n):
    j = n - (i + 1)
    
    if a[j] == 0 and b[j] == 0:
        Pk = ((2*Pkp1+m-1) % M) * Mi * Ti
    elif a[j] == 0:
        Pk = ((m-b[j]+Pkp1) % M) * Mi
    elif b[j] == 0:
        Pk = ((Pkp1+a[j]-1) % M) * Mi
    else:
        if a[j] > b[j]:
            Pk = 1
        elif a[j] < b[j]:
            Pk = 0
        else:
            Pk = Pkp1

    Pkp1 = Pk

print(Pk % M)


import sys

MOD = 1000000007

def solve(io):
  N = io.readInt()
  M = io.readInt()
  S = io.readIntArray(N)
  T = io.readIntArray(N)
  
  P = 0
  Q = 1
  A = 1
  B = 1
  for i in range(0, N):
    if S[i] == 0 or T[i] == 0:
      if S[i] == 0 and T[i] == 0:
        gtTop = M * (M - 1) * A
        gtBot = 2 * M * M * B
        eqTop = 1
        eqBot = M
      elif S[i] == 0:
        gtTop = (M - T[i]) * A
        gtBot = M * B
        eqTop = 1
        eqBot = M
      elif T[i] == 0:
        gtTop = (S[i] - 1) * A
        gtBot = M * B
        eqTop = 1
        eqBot = M
    elif S[i] > T[i]:
      gtTop = A
      gtBot = B
      eqTop = 0
      eqBot = 1
    elif S[i] < T[i]:
      gtTop = 0
      gtBot = 1
      eqTop = 0
      eqBot = 1
    elif S[i] == T[i]:
      gtTop = 0
      gtBot = 1
      eqTop = 1
      eqBot = 1
    P, Q = P * gtBot + gtTop * Q, Q * gtBot
    P %= MOD
    Q %= MOD
    A = (A * eqTop) % MOD
    B = (B * eqBot) % MOD
    if S[i] != 0 and T[i] != 0 and S[i] != T[i]:
      break
  io.println((P * modinv(Q, MOD)) % MOD)

def extended_gcd(aa, bb):
    lastremainder, remainder = abs(aa), abs(bb)
    x, lastx, y, lasty = 0, 1, 1, 0
    while remainder:
        lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder)
        x, lastx = lastx - quotient*x, x
        y, lasty = lasty - quotient*y, y
    return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1)
 
def modinv(a, m):
	g, x, _ = extended_gcd(a, m)
	if g != 1:
		raise ValueError
	return x % m

# +---------------------+
# | TEMPLATE CODE BELOW |
# |    DO NOT MODIFY    |
# +---------------------+

# TODO: maybe reading byte-by-byte is faster than reading and parsing tokens.
class IO:
  input = None
  output = None
  raw = ""
  buf = []
  pos = 0
  
  def __init__(self, inputStream, outputStream):
    self.input = inputStream
    self.output = outputStream
  
  def readToBuffer(self):
    self.raw = self.input.readline().rstrip('\n')
    self.buf = self.raw.split()
    self.pos = 0
  
  def readString(self):
    while self.pos == len(self.buf):
      self.readToBuffer()
    ans = self.buf[self.pos]
    self.pos += 1
    return ans
  
  def readInt(self):
    return int(self.readString())
  
  def readFloat(self):
    return float(self.readString())
  
  def readStringArray(self, N, offset = 0):
    arr = [ None ] * offset
    for _ in range(0, N):
      arr.append(self.readString())
    return arr
  
  def readIntArray(self, N, offset = 0):
    arr = [ None ] * offset
    for _ in range(0, N):
      arr.append(self.readInt())
    return arr
  
  def readFloatArray(self, N, offset = 0):
    arr = [ None ] * offset
    for _ in range(0, N):
      arr.append(self.readFloat())
    return arr
  
  def readLine(self):
    while self.pos == len(self.buf):
      self.readToBuffer()
    if self.pos > 0:
      raise ValueError("Cannot call readline in the middle of a line.")
    return self.raw
  
  def print(self, s):
    self.output.write(str(s))
  
  def println(self, s):
    self.print(s)
    self.print('\n')
  
  def flushOutput(self):
    self.output.flush()

pythonIO = IO(sys.stdin, sys.stdout)
solve(pythonIO)
pythonIO.flushOutput()


mod = 1000000007
def pow(a,b,c):
    n = 1
    m = a
    v = b
    if a == 1:
        return 1
    while v > 0:
        if v%2 == 1:
            n *= m%c
            n %= c
        m *= m
        m %= c
        v //= 2
    return n%c

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

a = list(map(int, input().split()))
b = list(map(int, input().split()))
z = [0 for i in range(n+10)]

for i in range(n)[::-1]:
    z[i] = z[i+1]
    if a[i] == 0:
        z[i] += 1
    if b[i] == 0:
        z[i] += 1
ans = 0
cmb = 1

if z[0] == 0:
    if a > b:
        print(1)
    else:
        print(0)
    quit()
inverse_two = pow(2,mod-2,mod)
com = (m*(m-1))%mod*inverse_two%mod
for i in range(n):
    if a[i] != 0 and b[i] != 0:
        if a[i] > b[i]:
            ans += cmb*pow(m,z[i+1],mod)%mod
        break
    elif a[i] == 0 and b[i] == 0:
        ans += (cmb*com)%mod*pow(m,z[i+1],mod)%mod
        cmb *= m
        cmb %= mod
    elif a[i] == 0:
        ans += (cmb*(m-b[i]))%mod*pow(m,z[i+1],mod)%mod
    elif b[i] == 0:
        ans += (cmb*(a[i]-1))%mod*pow(m,z[i+1],mod)%mod
    ans %= mod

k = pow(m,z[0],mod)
k = pow(k,mod-2,mod)
ans *= k
ans %= mod
print(ans)

mod = 1000000007
def pow(a,b,c):
    n = 1
    m = a
    v = b
    if a == 1:
        return 1
    while v > 0:
        if v%2 == 1:
            n *= m%c
            n %= c
        m *= m
        m %= c
        v //= 2
    return n%c

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

a = list(map(int, input().split()))
b = list(map(int, input().split()))
z = [0 for i in range(n+10)]

for i in range(n)[::-1]:
    z[i] = z[i+1]
    if a[i] == 0:
        z[i] += 1
    if b[i] == 0:
        z[i] += 1
ans = 0
cmb = 1

if z[0] == 0:
    if a > b:
        print(1)
    else:
        print(0)
    quit()
inverse_two = pow(2,mod-2,mod)
com = (m*(m-1))%mod*inverse_two%mod
for i in range(n):
    if a[i] != 0 and b[i] != 0:
        if a[i] > b[i]:
            ans += cmb*pow(m,z[i+1],mod)%mod
        break
    elif a[i] == 0 and b[i] == 0:
        ans += (cmb*com)%mod*pow(m,z[i+1],mod)%mod
        cmb *= m
        cmb %= mod
    elif a[i] == 0:
        ans += (cmb*(m-b[i]))%mod*pow(m,z[i+1],mod)%mod
    elif b[i] == 0:
        ans += (cmb*(a[i]-1))%mod*pow(m,z[i+1],mod)%mod
    ans %= mod

k = pow(m,z[0],mod)
k = pow(k,mod-2,mod)
ans *= k
ans %= mod
print(ans)

def _pow(a, d, md):
    res = 1
    a = a % md
    while d > 0:
        if d&1: res = res * a % md
        d >>= 1
        a = a*a % md
    return res

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mod = 1000000007
p = 1
q = 1
dp = [[0 for i in range(n + 1)] for j in range(2)]
base = 1
dp[0][-1] = 1
dp[1][-1] = 0
for i in range(0, n):
    if a[i] == 0 and b[i] != 0:
        dp[0][i] = dp[0][i - 1] % mod
        dp[1][i] = (dp[1][i - 1] * m + dp[0][i - 1]*(m - b[i])) % mod
        base *= m
        base %= mod
    elif a[i] != 0 and b[i] == 0:
        dp[0][i] = dp[0][i - 1] % mod
        dp[1][i] = (dp[1][i - 1] * m + dp[0][i - 1] * (a[i] - 1)) % mod
        base *= m
        base %= mod
    elif a[i] == 0 and b[i] == 0:
        dp[0][i] = dp[0][i - 1] * m % mod
        dp[1][i] = (dp[1][i - 1] * m * m) % mod
        dp[1][i] += (dp[0][i - 1] * (m - 1) * m // 2) % mod
        base *= m * m
        base %= mod
    elif a[i] != 0 and b[i] != 0:
        if a[i] != b[i]:
            dp[0][i] = 0
            dp[1][i] = dp[1][i - 1] % mod
            if a[i] > b[i]:
                dp[1][i] += dp[0][i - 1] % mod
        else:
            dp[0][i] = dp[0][i - 1] % mod
            dp[1][i] = dp[1][i - 1] % mod
    #print(dp[0][i], dp[1][i])        

p = dp[1][n - 1]
q = base
p %= mod
q %= mod
q = _pow(q, mod - 2, mod)
print(p*q % mod)

def _pow(a, d, md):
    res = 1
    a = a % md
    while d > 0:
        if d&1: res = res * a % md
        d >>= 1
        a = a*a % md
    return res

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mod = 1000000007
p = 1
q = 1
dp = [[0 for i in range(n + 1)] for j in range(2)]
base = 1
dp[0][-1] = 1
dp[1][-1] = 0
for i in range(0, n):
    if a[i] == 0 and b[i] != 0:
        dp[0][i] = dp[0][i - 1] % mod
        dp[1][i] = (dp[1][i - 1] * m + dp[0][i - 1]*(m - b[i])) % mod
        base *= m
        base %= mod
    elif a[i] != 0 and b[i] == 0:
        dp[0][i] = dp[0][i - 1] % mod
        dp[1][i] = (dp[1][i - 1] * m + dp[0][i - 1] * (a[i] - 1)) % mod
        base *= m
        base %= mod
    elif a[i] == 0 and b[i] == 0:
        dp[0][i] = dp[0][i - 1] * m % mod
        dp[1][i] = (dp[1][i - 1] * m * m) % mod
        dp[1][i] += (dp[0][i - 1] * (m - 1) * m // 2) % mod
        base *= m * m
        base %= mod
    elif a[i] != 0 and b[i] != 0:
        if a[i] != b[i]:
            dp[0][i] = 0
            dp[1][i] = dp[1][i - 1] % mod
            if a[i] > b[i]:
                dp[1][i] += dp[0][i - 1] % mod
        else:
            dp[0][i] = dp[0][i - 1] % mod
            dp[1][i] = dp[1][i - 1] % mod
    #print(dp[0][i], dp[1][i])        

p = dp[1][n - 1]
q = base
p %= mod
q %= mod
q = _pow(q, mod - 2, mod)
print(p*q % mod)



c_mod = 1000000007

def InverseMod(b: int, m: int):
    b %= m
    if b == 0 or b == 1: return b

    a = m
    qs = []
    while True:
        q = a // b
        r = a % b
        if (r == 0): break
        a = b
        b = r
        qs.append(q)

    qA = 1
    qB = -qs[len(qs) - 1]
    for i in range(len(qs) - 2, -1, -1):
        t = qA
        qA = qB
        qB = qB*(-qs[i]) + t
    
    qB %= m
    if qB < 0: 
        qB = m + qB
    return qB

def save_mul(a, b):
    return (a * b) % c_mod


class Fraction:
    def __init__(self, a: int, b: int, dBase: int, dPow: int):
        self.a, self.b, self.dBase, self.dPow = a, b, dBase, dPow
    
    def __add__(self, other):
        f = Fraction(self.a, self.b, self.dBase, self.dPow)
        if f.dPow > other.dPow:
            f.a += other.a * (f.dBase ** (f.dPow - other.dPow))
            f.a %= c_mod
        elif f.dPow < other.dPow:
            f.a = f.a * (f.dBase ** (other.dPow - f.dPow)) + other.a
            f.b = other.b
            f.dPow = other.dPow
            f.a %= c_mod
        else:
            f.a += other.a
            f.a %= c_mod
        return f
    
    def __mul__(self, other):
        f = Fraction(self.a, self.b, self.dBase, self.dPow)
        f.a *= other.a
        f.b *= other.b
        f.dPow += other.dPow
        f.a %= c_mod
        f.b %= c_mod
        return f
    
    def __str__(self):
        return "{0}/{1}".format(self.a, self.b)



def main():
    '''f1 = Fraction(6, 5)
    f2 = Fraction(5, 5)
    f1.dPow = 2
    f1.b = 25
    print(f1 + f2)
    print(f1)'''

    s = str(input()).split()
    n, m = int(s[0]), int(s[1])
    s1 = str(input()).split()
    s2 = str(input()).split()
    r = Fraction(0, 1, m, 0)
    k = Fraction(1, 1, m, 0)

    for i in range(n):
        u = int(s1[i])
        d = int(s2[i])
        if u == 0 and d == 0:
            f = Fraction(int(m * (m - 1) / 2), m * m, m, 2)
            f = f * k
            r = r + f
            k.b = save_mul(k.b, m)
            k.dPow += 1
            continue
        elif u == 0:
            f = Fraction(m - d, m, m, 1)
            f = f * k
            r = r + f
            k.b = save_mul(k.b, m)
            k.dPow += 1
            continue
        elif d == 0:
            f = Fraction(u - 1, m, m, 1)
            f = f * k
            r = r + f
            k.b = save_mul(k.b, m)
            k.dPow += 1
            continue
        
        if u > d:
            r = r + k

        if u != d: break
    
    print(save_mul(r.a, InverseMod(r.b, c_mod)))


main()


import sys
input = sys.stdin.readline

mod = 10**9 + 7
def invmod(x, mod):
    return pow(x, mod-2, mod)

n, m = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
dp = [0] * (n+1)
for i in range(n-1, -1, -1):
    if a[i] == 0 and b[i] == 0:
        sp, sq = m * m, m
        tp, tq = m * m, m * (m-1) // 2
    elif a[i] == 0:
        sp, sq = m, 1
        tp, tq = m, m - b[i]
    elif b[i] == 0:
        sp, sq = m, 1
        tp, tq = m, a[i] - 1
    else:
        sp, sq = 1, 1 if a[i] == b[i] else 0
        tp, tq = 1, 1 if a[i] > b[i] else 0
    dp[i] = sq * invmod(sp, mod) * dp[i + 1]
    dp[i] += tq * invmod(tp, mod)
    dp[i] %= mod
ans = dp[0]
print(ans)