#include <bits/stdc++.h>
using namespace std;
long long n, m, a[100000 + 10], b[100000 + 10], nr, ans;
long long lgput(long long a, long long n) {
  if (!n) return 1;
  if (n % 2 == 0) return lgput(a * a % 1000000007, n / 2);
  return a * lgput(a * a % 1000000007, n / 2) % 1000000007;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (!a[i]) nr++;
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    if (!b[i]) nr++;
  }
  long long total = lgput(m, nr), curr = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] && b[i]) {
      if (a[i] > b[i]) {
        ans = (ans + lgput(m, nr) * curr % 1000000007) % 1000000007;
        break;
      } else if (a[i] < b[i])
        break;
      else
        continue;
    } else if (a[i] && !b[i]) {
      nr--;
      ans = (ans + (a[i] - 1) * lgput(m, nr) % 1000000007 * curr % 1000000007) %
            1000000007;
    } else if (!a[i] && b[i]) {
      nr--;
      ans = (ans + (m - b[i]) * lgput(m, nr) % 1000000007 * curr % 1000000007) %
            1000000007;
    } else {
      nr -= 2;
      ans = (ans + m * (m - 1) % 1000000007 * lgput(2LL, 1000000007 - 2) %
                       1000000007 * lgput(m, nr) % 1000000007 * curr %
                       1000000007) %
            1000000007;
      curr = curr * m % 1000000007;
    }
  }
  cout << ans * lgput(total, 1000000007 - 2) % 1000000007 << '\n';
  return 0;
}
