#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int N, M;
int C1[maxn], C2[maxn];
int S1[maxn], S2[maxn];
inline int mul_mod(int x, int y) { return 1LL * x * y % mod; }
int pow_mod(int x, int n) {
  int ret = 1;
  while (n) {
    if (n & 1) ret = mul_mod(ret, x);
    x = mul_mod(x, x);
    n >>= 1;
  }
  return ret;
}
void init() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) scanf("%d", &S1[i]);
  for (int i = 1; i <= N; i++) scanf("%d", &S2[i]);
  C1[N + 1] = C2[N + 1] = 1;
  for (int i = N; i; i--) {
    if (S1[i])
      C1[i] = C1[i + 1];
    else
      C1[i] = mul_mod(C1[i + 1], M);
    if (S2[i])
      C2[i] = C2[i + 1];
    else
      C2[i] = mul_mod(C2[i + 1], M);
  }
}
int solve(int d) {
  if (d > N) return 0;
  int ret = 0;
  if (S1[d] == 0 && S2[d] == 0) {
    ret = (ret + mul_mod(M, solve(d + 1))) % mod;
    int c = mul_mod(mul_mod(M, M - 1), pow_mod(2, mod - 2));
    ret = (ret + mul_mod(c, mul_mod(C1[d + 1], C2[d + 1]))) % mod;
  } else if (S1[d] == 0) {
    ret = (ret + solve(d + 1)) % mod;
    ret = (ret + 1LL * (M - S2[d]) * mul_mod(C1[d + 1], C2[d + 1])) % mod;
  } else if (S2[d] == 0) {
    ret = (ret + solve(d + 1)) % mod;
    ret = (ret + 1LL * (S1[d] - 1) * mul_mod(C1[d + 1], C2[d + 1])) % mod;
  } else if (S1[d] && S2[d]) {
    if (S1[d] > S2[d])
      ret = mul_mod(C1[d], C2[d]);
    else if (S1[d] < S2[d])
      ret = 0;
    else
      ret = solve(d + 1);
  }
  return ret;
}
int main() {
  init();
  int Q = mul_mod(C1[1], C2[1]);
  int invQ = pow_mod(Q, mod - 2);
  int P = solve(1);
  printf("%d\n", mul_mod(P, invQ));
  return 0;
}
