#include <bits/stdc++.h>
long long modulo = 1000000007;
std::vector<std::vector<long long>> vect;
long long fastmul(long long num, long long pow) {
  if (pow == 0) return 1;
  if (pow % 2 == 0) {
    long long numsq = fastmul(num, pow / 2);
    return (numsq * numsq) % modulo;
  } else {
    return (num * fastmul(num, pow - 1)) % modulo;
  }
}
std::vector<std::vector<long long>> zerossuff;
long long find(long long pos, long long n, long long m) {
  if (pos >= n) return 0;
  if (vect[0][pos] != 0 && vect[0][pos] < vect[1][pos]) return 0;
  if (vect[0][pos] != 0 && vect[0][pos] == vect[1][pos])
    return find(pos + 1, n, m);
  if (vect[1][pos] != 0 && vect[0][pos] > vect[1][pos]) {
    long long cnt = zerossuff[0][pos] + zerossuff[1][pos];
    long long ret = fastmul(m, cnt);
    return ret;
  }
  if (vect[0][pos] != 0) {
    long long ans = find(pos + 1, n, m);
    long long cnt = zerossuff[0][pos + 1] + zerossuff[1][pos + 1];
    long long ret = fastmul(m, cnt);
    ret = (ret * (vect[0][pos] - 1)) % modulo;
    ret += ans;
    ret = ret % modulo;
    return ret;
  }
  if (vect[1][pos] != 0) {
    long long ans = find(pos + 1, n, m);
    long long cnt = zerossuff[0][pos + 1] + zerossuff[1][pos + 1];
    long long ret = fastmul(m, cnt);
    ret = (ret * (m - vect[1][pos])) % modulo;
    ret += ans;
    ret = ret % modulo;
    return ret;
  }
  long long ans = find(pos + 1, n, m);
  long long cnt = zerossuff[0][pos + 1] + zerossuff[1][pos + 1];
  long long ret = fastmul(m, cnt);
  long long vars = m * (m - 1) / 2;
  ret = (ret * vars) % modulo;
  ret += ans * m;
  ret = ret % modulo;
  return ret;
}
int main() {
  long long n, m;
  std::cin >> n >> m;
  vect.resize(2, std::vector<long long>(n));
  zerossuff.resize(2, std::vector<long long>(n + 1, 0));
  for (long long i = 0; i < 2; ++i) {
    for (long long j = 0; j < n; ++j) {
      std::cin >> vect[i][j];
    }
  }
  for (long long i = 0; i < 2; ++i) {
    for (long long j = n - 1; j >= 0; --j) {
      if (vect[i][j] == 0) zerossuff[i][j] = 1;
      zerossuff[i][j] += zerossuff[i][j + 1];
    }
  }
  long long good_vars = find(0, n, m);
  long long cnt = zerossuff[0][0] + zerossuff[1][0];
  long long all_vars = fastmul(m, cnt);
  long long all_rev = fastmul(all_vars, modulo - 2);
  long long ans = all_rev * good_vars;
  ans = ans % modulo;
  std::cout << ans;
}
