#include <bits/stdc++.h>
using namespace std;
void readi(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = v * 10 + c - '0';
  while (isdigit(c = getchar())) v = v * 10 + c - '0';
  x = v * f;
}
void readc(char &x) {
  char c;
  while ((c = getchar()) == ' ')
    ;
  x = c;
}
void writes(string s) { puts(s.c_str()); }
void writeln() { writes(""); }
void writei(int x) {
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
inline long long inc(int &x) { return ++x; }
inline long long inc(long long &x) { return ++x; }
inline long long inc(int &x, long long y) { return x += y; }
inline long long inc(long long &x, long long y) { return x += y; }
inline double inc(double &x, double y) { return x += y; }
inline long long dec(int &x) { return --x; }
inline long long dec(long long &x) { return --x; }
inline long long dec(int &x, long long y) { return x -= y; }
inline long long dec(long long &x, long long y) { return x -= y; }
inline double dec(double &x, double y) { return x -= y; }
inline long long mul(int &x) { return x = ((long long)x) * x; }
inline long long mul(long long &x) { return x = x * x; }
inline long long mul(int &x, long long y) { return x *= y; }
inline long long mul(long long &x, long long y) { return x *= y; }
inline double mul(double &x, double y) { return x *= y; }
inline long long divi(int &x) { return x = sqrt(x); }
inline long long divi(long long &x) { return x = sqrt(x); }
inline long long divi(int &x, long long y) { return x /= y; }
inline long long divi(long long &x, long long y) { return x /= y; }
inline double divi(double &x, double y) { return x /= y; }
inline long long mod(int &x, long long y) { return x %= y; }
inline long long mod(long long &x, long long y) { return x %= y; }
long long n, m, i, j, a[100005], b[100005], suf[100005], wyx = 1e9 + 7, ans,
                                                         pre;
long long qp(long long x, long long y) {
  if (y == 0) return 1;
  if (y == 1) return x;
  long long z = qp(x, y / 2);
  z = z * z % wyx;
  if (y & 1) z = z * x % wyx;
  return z;
}
int main() {
  ios_base::sync_with_stdio(false);
  ;
  cin >> n >> m;
  if (1 <= (n))
    for (((i)) = (1); ((i)) <= ((n)); ((i))++) cin >> a[i];
  if (1 <= (n))
    for (((i)) = (1); ((i)) <= ((n)); ((i))++) cin >> b[i];
  if (1 <= (n))
    for (((i)) = (1); ((i)) <= ((n)); ((i))++) suf[i] += (a[i] == 0);
  if (1 <= (n))
    for (((i)) = (1); ((i)) <= ((n)); ((i))++) suf[i] += (b[i] == 0);
  if ((n) >= 1)
    for (((i)) = ((n)); ((i)) >= (1); ((i))--) suf[i] += suf[i + 1];
  pre = 1;
  if (1 <= (n))
    for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
      if (a[i] != 0 && b[i] != 0) {
        if (a[i] == b[i]) continue;
        if (a[i] > b[i]) {
          inc(ans, qp(m, suf[i + 1]) % wyx * pre % wyx);
          mod(ans, wyx);
        }
        break;
      }
      if (a[i] == 0 && b[i] == 0) {
        inc(ans, (m * m - m) / 2 % wyx * pre % wyx * qp(m, suf[i + 1]) % wyx);
        mod(ans, wyx);
        mul(pre, m);
        mod(pre, wyx);
        continue;
      }
      if (a[i] == 0 && b[i] != 0) {
        inc(ans, (m - b[i]) * pre % wyx * qp(m, suf[i + 1]) % wyx);
        mod(ans, wyx);
        continue;
      }
      if (a[i] != 0 && b[i] == 0) {
        inc(ans, (a[i] - 1) * pre % wyx * qp(m, suf[i + 1]) % wyx);
        mod(ans, wyx);
        continue;
      }
    }
  cout << (ans * qp(qp(m, suf[1]) % wyx, wyx - 2) % wyx + wyx) % wyx << endl;
  fprintf(stderr, "%I64d/%I64d\n", ans, qp(m, suf[1]));
  return 0;
}
