#include <bits/stdc++.h>
using namespace std;
long long a[200000], b[200000];
const long long mod = 1e9 + 7;
long long inv(long long x) {
  long long ans = 1;
  long long tmp = x;
  long long t = mod - 2;
  while (t) {
    if (t & 1) ans = ans * tmp % mod;
    tmp = tmp * tmp % mod;
    t >>= 1;
  }
  return ans;
}
int main(void) {
  long long n, m;
  scanf("%lld%lld", &n, &m);
  long long tmp = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] == 0) tmp = tmp * m % mod;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &b[i]);
    if (b[i] == 0) tmp = tmp * m % mod;
  }
  long long invm = inv(m);
  long long invtmp = inv(tmp);
  long long ans = 0;
  long long k = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0 && b[i] == 0) {
      tmp = tmp * invm % mod;
      tmp = tmp * invm % mod;
      long long ttt = m * (m - 1) / 2;
      ans = ans + ttt % mod * tmp % mod * k % mod;
      ans %= mod;
      k = k * m % mod;
      continue;
    }
    if (a[i] == 0) {
      long long ttt = m - b[i];
      tmp = tmp * invm % mod;
      ans = ans + tmp * k % mod * ttt % mod;
      ans %= mod;
      continue;
    }
    if (b[i] == 0) {
      long long ttt = a[i] - 1;
      tmp = tmp * invm % mod;
      ans = ans + tmp * k % mod * ttt % mod;
      ans %= mod;
      continue;
    }
    if (a[i] == b[i]) continue;
    if (a[i] > b[i]) {
      ans = ans + k * tmp % mod;
      ans %= mod;
    }
    break;
  }
  printf("%lld\n", ans * invtmp % mod);
  return 0;
}
