#include <bits/stdc++.h>
namespace fastIO {
bool IOerror = 0;
inline char nc() {
  static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, 100000, stdin);
    if (pend == p1) {
      IOerror = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool blank(char ch) {
  return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
  char ch;
  while (blank(ch = nc()))
    ;
  if (IOerror) return;
  for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0')
    ;
}
};  // namespace fastIO
template <class T>
inline T lowbit(T x) {
  return x & (-x);
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline bool scan(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
const double pi = 3.14159265358979323846264338327950288L;
using namespace std;
const long long mod = 1000000007;
const long long INF = 1e18;
int n, m, a[100005], b[100005];
long long last[100005];
long long qpow(long long a, long long b) {
  if (!b) return 1;
  long long ans = qpow(a, b >> 1);
  ans = ans * ans % mod;
  if (b & 1) ans = ans * a % mod;
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  last[n + 1] = 0;
  for (int i = n; i >= 1; i--) {
    last[i] = last[i + 1];
    if (!a[i]) last[i]++;
    if (!b[i]) last[i]++;
  }
  long long pre = 1, ans = 0, tot = qpow(m, last[1]);
  for (int i = 1; i <= n; i++) {
    if (a[i] && b[i]) {
      if (a[i] == b[i])
        continue;
      else if (a[i] > b[i]) {
        ans += pre * qpow(m, last[i + 1]) % mod;
        ans %= mod;
      }
      break;
    } else if (!a[i] && !b[i]) {
      ans += pre * (1LL * m * (m - 1) / 2 % mod) % mod * qpow(m, last[i + 1]) %
             mod;
      ans %= mod;
      pre = pre * m % mod;
    } else if (!a[i]) {
      ans += pre * (m - b[i]) % mod * qpow(m, last[i + 1]) % mod;
      ans %= mod;
    } else {
      ans += pre * (a[i] - 1) % mod * qpow(m, last[i + 1]) % mod;
      ans %= mod;
    }
  }
  cout << ans * qpow(tot, mod - 2) % mod << endl;
  return 0;
}
