#include <bits/stdc++.h>
using namespace std;
const int maxn = 100009;
const int mod = 1e9 + 7;
int n, m;
int a[maxn], b[maxn], s[maxn];
long long ft[maxn << 1];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long qpow(long long x, int y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int main() {
  n = read();
  m = read();
  long long t = 1ll * m * (m - 1) / 2 % mod;
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  for (int i = n; i >= 1; i--) s[i] = s[i + 1] + (a[i] == 0) + (b[i] == 0);
  ft[0] = 1;
  for (int i = 1; i <= n + n; i++) ft[i] = ft[i - 1] * m % mod;
  long long sum = 0;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] && b[i]) {
      if (a[i] > b[i]) {
        sum = (sum + ft[s[i + 1]] * ft[cnt] % mod) % mod;
        break;
      }
      if (a[i] < b[i]) break;
    } else if (a[i] && b[i] == 0)
      sum = (sum + (a[i] - 1) * ft[s[i + 1]] % mod * ft[cnt]) % mod;
    else if (a[i] == 0 && b[i])
      sum = (sum + (m - b[i]) * ft[s[i + 1]] % mod * ft[cnt]) % mod;
    else {
      sum = (sum + t * ft[s[i + 1]] % mod * ft[cnt]) % mod;
      cnt++;
    }
  }
  printf("%lld\n", sum * qpow(ft[s[1]], mod - 2) % mod);
  return 0;
}
