#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
  while (b) b ^= a ^= b ^= a %= b;
  return a;
}
int MAX = numeric_limits<int>::max();
const int N = 1e6 + 5;
long long po(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y % 2 == 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long invm(long long x, long long p) { return po(x, p - 2, p); }
long long m;
int n;
long long a[N], b[N], suf[N];
long long ans = 0, ansd = 1;
int main() {
  scanf("%d", &n);
  scanf("%lld", &m);
  ansd = invm(1, 1000000007);
  for (int i = 1; i < n + 1; i++) {
    scanf("%lld", &a[i]);
    if (a[i] == 0) {
      ansd = (ansd * m) % 1000000007;
    }
  }
  for (int i = 1; i < n + 1; i++) {
    scanf("%lld", &b[i]);
    if (b[i] == 0) {
      ansd = (ansd * m) % 1000000007;
    }
  }
  if (m == 1) {
    cout << 0 << "\n";
    return 0;
  }
  suf[n + 1] = 1;
  for (int i = n; i >= 1; i--) {
    suf[i] = suf[i + 1];
    if (a[i] == 0) {
      suf[i] = (suf[i] * m) % 1000000007;
    }
    if (b[i] == 0) {
      suf[i] = (suf[i] * m) % 1000000007;
    }
  }
  int f = 0, first = 0;
  long long inv2 = invm(2, 1000000007);
  long long dd = 1;
  for (int i = 1; i < n + 1; i++) {
    if (a[i] != b[i] && a[i] != 0 && b[i] != 0) {
      if (first == 0 && a[i] < b[i]) {
        ans = 0;
      } else if (a[i] > b[i]) {
        first = 1;
        ans = (ans + suf[i + 1] * dd) % 1000000007;
      }
      break;
    } else if (a[i] == 0 && b[i] == 0) {
      first = 1;
      long long d = (m * ((m - 1 + 1000000007) % 1000000007)) % 1000000007;
      d = (d * inv2) % 1000000007;
      d = (d * suf[i + 1]) % 1000000007;
      d = (d * dd) % 1000000007;
      ans = (ans + d) % 1000000007;
      dd = (dd * m) % 1000000007;
    } else if (a[i] == 0) {
      if (b[i] < m) {
        first = 1;
        long long d = (m - b[i] + 1000000007) % 1000000007;
        d = (d * suf[i + 1]) % 1000000007;
        d = (d * dd) % 1000000007;
        ans = (ans + d) % 1000000007;
      }
    } else if (b[i] == 0) {
      if (a[i] > 1) {
        first = 1;
        long long d = (a[i] - 1 + 1000000007) % 1000000007;
        d = (d * suf[i + 1]) % 1000000007;
        d = (d * dd) % 1000000007;
        ans = (ans + d) % 1000000007;
      }
    }
  }
  if (first == 0)
    cout << "0\n";
  else
    cout << ((ans * invm(ansd, 1000000007)) % 1000000007) << "\n";
}
