#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000;
const long long int mod = (1e9 + 7);
const long long int inf = 1e9;
long long int n, m, a[maxn + 5], b[maxn + 5], zero1[maxn + 5], zero2[maxn + 5];
void extgcd(long long int a, long long int b, long long int &x,
            long long int &y) {
  if (!b) {
    x = 1, y = 0;
  } else {
    extgcd(b, a % b, y, x);
    y -= x * (a / b);
  }
}
long long int mod_inverse(long long int a, long long int m) {
  long long int x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}
long long int Pow_mod(long long int m, long long int t) {
  if (t == 0) return 1;
  long long int x = Pow_mod(m, t / 2);
  long long int ans = (x * x) % mod;
  if (t % 2) ans = (ans * m) % mod;
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i], zero1[i] = zero1[i - 1] + (a[i] == 0);
  for (int i = 1; i <= n; i++)
    cin >> b[i], zero2[i] = zero2[i - 1] + (b[i] == 0);
  long long int P = 0, Q = 0, F = 1;
  Q = Pow_mod(m, zero1[n] + zero2[n]);
  for (int i = 1; i <= n; i++) {
    long long int tot = zero1[n] - zero1[i] + zero2[n] - zero2[i];
    if (a[i] && b[i] && (a[i] < b[i] || a[i] > b[i])) {
      if (a[i] > b[i]) {
        P = (P + F * Pow_mod(m, tot) % mod) % mod;
      }
      break;
    }
    if (a[i] == 0 && b[i]) {
      P = (P + (F * (m - b[i]) % mod * Pow_mod(m, tot)) % mod) % mod;
    }
    if (a[i] && b[i] == 0) {
      P = (P + (F * (a[i] - 1) % mod * Pow_mod(m, tot)) % mod) % mod;
    }
    if (!a[i] && !b[i]) {
      P = (P + ((F * (m * (m - 1) / 2)) % mod) * Pow_mod(m, tot) % mod) % mod;
      F = (F * m) % mod;
    }
  }
  Q = Q % mod;
  long long int inv = Pow_mod(Q % mod, mod - 2);
  long long int ans = (P * inv * 1ll) % mod;
  cout << ans << endl;
  return 0;
}
