#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 7;
int n, m, cc, a[100005], b[100005];
long long p[200005];
long long pw(long long x, long long b) {
  long long ans = 1ll;
  while (b) {
    if (b & 1) ans = ans * x % md;
    x = x * x % md;
    b >>= 1;
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++)
    if (!a[i]) cc++;
  for (int i = 0; i < n; i++) scanf("%d", &b[i]);
  for (int i = 0; i < n; i++)
    if (!b[i]) cc++;
  p[0] = 1ll;
  for (int i = 1; i < 200005; i++) p[i] = p[i - 1] * m % md;
  long long ans = 0, lst = 1ll, cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!a[i]) cnt++;
    if (!b[i]) cnt++;
    if (!a[i] && !b[i]) {
      ans = (ans + lst * p[cc - cnt] % md * (1ll * m * (m - 1) / 2 % md) % md) %
            md;
      lst = lst * m % md;
    } else if (!a[i])
      ans = (ans + lst * p[cc - cnt] % md * (m - b[i]) % md) % md;
    else if (!b[i])
      ans = (ans + lst * p[cc - cnt] % md * (a[i] - 1) % md) % md;
    else if (a[i] ^ b[i]) {
      if (a[i] > b[i]) ans = (ans + lst * p[cc - cnt] % md) % md;
      break;
    }
  }
  ans = ans * pw(p[cc], md - 2) % md;
  printf("%I64d\n", ans);
  return 0;
}
