#include <bits/stdc++.h>
using namespace std;
const long long P = 1000000007;
long long modinv(long long a, long long b) {
  long long b0 = b, t, q;
  long long x0 = 0, x1 = 1;
  if (b == 1) return 1;
  while (a > 1) {
    q = a / b;
    t = b, b = a % b, a = t;
    t = x0, x0 = x1 - q * x0, x1 = t;
  }
  return x1;
}
tuple<long long, long long> s(const vector<long long> &a,
                              const vector<long long> &b, long long m, int i);
tuple<long long, long long> r(const vector<long long> &a,
                              const vector<long long> &b, long long m, int i,
                              long long p, long long q) {
  long long p2, q2;
  tie(p2, q2) = s(a, b, m, i + 1);
  q2 = (q2 * m) % P;
  p = ((p * q2) % P + (p2 * q) % P) % P;
  q = (q * q2) % P;
  return make_tuple(p, q);
}
tuple<long long, long long> s(const vector<long long> &a,
                              const vector<long long> &b, long long m, int i) {
  if (i == a.size()) return make_tuple(0, 1);
  if (a[i] > 0 && b[i] > 0) {
    if (a[i] > b[i]) return make_tuple(1, 1);
    if (a[i] < b[i]) return make_tuple(0, 1);
    return s(a, b, m, i + 1);
  }
  if (a[i] == 0 && b[i] == 0) {
    long long p = m * (m - 1) / 2 % P;
    long long q = m * m % P;
    return r(a, b, m, i, p, q);
  }
  if (a[i] == 0) {
    long long p = m - b[i];
    long long q = m;
    return r(a, b, m, i, p, q);
  }
  long long p = a[i] - 1;
  long long q = m;
  return r(a, b, m, i, p, q);
}
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n);
  vector<long long> b(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  long long p, q;
  tie(p, q) = s(a, b, m, 0);
  long long r = (modinv(q, P) * p) % P;
  if (r < 0) r += P;
  cout << r << endl;
}
