#include <bits/stdc++.h>
using namespace std;
long double e = 1e-8;
long long mod = 1e9 + 7;
long long bin_pow(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2) {
    return a * bin_pow(a, b - 1) % mod;
  } else {
    long long x = bin_pow(a, b / 2) % mod;
    return x * x % mod;
  }
}
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  vector<long long> suf(n + 1);
  for (int i = n - 1; i >= 0; --i) {
    suf[i] = (a[i] == 0) + (b[i] == 0) + suf[i + 1];
  }
  long long p = 0, k = 1;
  long long pesos = 0;
  for (int i = 1; i <= m; ++i) {
    pesos = (pesos + (i - 1)) % mod;
  }
  for (int i = 0; i < n; ++i) {
    if (a[i] == 0 && b[i] == 0) {
      p = (p + pesos * bin_pow(m, suf[i + 1]) % mod * k % mod) % mod;
      k = k * m % mod;
    } else if (a[i] == 0) {
      p = (p + (m - b[i]) * bin_pow(m, suf[i + 1]) % mod * k % mod) % mod;
    } else if (b[i] == 0) {
      p = (p + (a[i] - 1) * bin_pow(m, suf[i + 1]) % mod * k % mod) % mod;
    } else if (a[i] != b[i]) {
      if (a[i] > b[i]) {
        p = (p + k * bin_pow(m, suf[i + 1]) % mod) % mod;
      }
      break;
    }
  }
  cout << p * bin_pow(bin_pow(m, suf[0]), mod - 2) % mod;
  return 0;
}
