#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
int s1[maxn], s2[maxn];
long long po(long long a, long long p) {
  long long ret = 1;
  while (p) {
    if (p & 1) ret = ret * a % mod;
    a = a * a % mod;
    p >>= 1;
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &s1[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &s2[i]);
  long long p = 0, p2 = 1;
  for (int i = 1; i <= n; i++) {
    if (s1[i] != s2[i]) {
      if (s1[i] && s2[i]) {
        if (s1[i] > s2[i]) {
          p = (p + p2) % mod;
        }
        break;
      } else if (s1[i]) {
        p2 = p2 * po(m, mod - 2) % mod;
        p = (p + p2 * (s1[i] - 1) % mod) % mod;
      } else if (s2[i]) {
        p2 = p2 * po(m, mod - 2) % mod;
        p = (p + p2 * (m - s2[i]) % mod) % mod;
      }
    } else if (!s1[i]) {
      p2 = p2 * po(m, mod - 2) % mod;
      p = (p + p2 * (m - 1) % mod * po(2, mod - 2) % mod) % mod;
    }
  }
  cout << p;
  return 0;
}
