#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10, mod = 1e9 + 7;
long long a[N], b[N];
long long ppow(long long x, long long y) {
  if (!y) return 1;
  if (y & 1) return (ppow(x, y - 1) * x) % mod;
  long long p = ppow(x, y >> 1);
  return (p * p) % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long j = 0; j < n; j++) cin >> b[j];
  long long p = 0, q = 1;
  for (long long i = n - 1; i >= 0; i--) {
    long long tp, tq;
    if (a[i] == 0)
      if (b[i] == 0)
        tp = m - 1, tq = 2 * m;
      else
        tp = m - b[i], tq = m;
    else if (b[i] == 0)
      tp = a[i] - 1, tq = m;
    if (a[i] != 0 && b[i] != 0) {
      if (a[i] > b[i])
        p = 1, q = 1;
      else if (a[i] < b[i])
        p = 0, q = 1;
    } else {
      q = (q * m) % mod;
      p = (p * tq + tp * q) % mod;
      q = (q * tq) % mod;
    }
  }
  cout << (p * ppow(q, mod - 2)) % mod;
}
