#include <bits/stdc++.h>
template <typename T>
class IntegerIterator
    : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
 public:
  explicit IntegerIterator(int value) : value(value) {}
  IntegerIterator& operator++() {
    ++value;
    return *this;
  }
  IntegerIterator operator++(int) {
    IntegerIterator copy = *this;
    ++value;
    return copy;
  }
  IntegerIterator& operator--() {
    --value;
    return *this;
  }
  IntegerIterator operator--(int) {
    IntegerIterator copy = *this;
    --value;
    return copy;
  }
  T operator*() const { return value; }
  bool operator==(IntegerIterator rhs) { return value == rhs.value; }
  bool operator!=(IntegerIterator rhs) { return !(*this == rhs); }

 private:
  T value;
};
template <typename T>
class IntegerRange {
 public:
  IntegerRange(T begin, T end) : begin_(begin), end_(end) {}
  IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); }
  IntegerIterator<T> end() const { return IntegerIterator<T>(end_); }

 private:
  T begin_;
  T end_;
};
template <typename T>
class ReversedIntegerRange {
 public:
  ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) {}
  std::reverse_iterator<IntegerIterator<T>> begin() const {
    return std::reverse_iterator<IntegerIterator<T>>(
        IntegerIterator<T>(begin_));
  }
  std::reverse_iterator<IntegerIterator<T>> end() const {
    return std::reverse_iterator<IntegerIterator<T>>(IntegerIterator<T>(end_));
  }

 private:
  T begin_;
  T end_;
};
template <typename T>
IntegerRange<T> range(T to) {
  ;
  return IntegerRange<T>(0, to);
}
template <typename T>
IntegerRange<T> range(T from, T to) {
  ;
  return IntegerRange<T>(from, to);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from) {
  ;
  return ReversedIntegerRange<T>(from, 0);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from, T to) {
  ;
  return ReversedIntegerRange<T>(from, to);
}
template <typename T>
T identity();
template <typename T, typename Enable = std::true_type>
struct impl__IdentityHelper {};
template <typename T>
struct impl__SampleIdentityHelper {
  static T identity(const T&) { return ::identity<T>(); }
};
template <typename T>
struct impl__IdentityHelper<T, typename std::is_arithmetic<T>::type> {
  static T identity() { return 1; }
};
template <typename T>
T identity(const T& sample) {
  return impl__SampleIdentityHelper<T>::identity(sample);
}
template <typename T>
T identity() {
  return impl__IdentityHelper<T>::identity();
}
template <bool normalize>
struct impl__Normalizator {};
template <>
struct impl__Normalizator<true> {
  static void softUp(long long& value, long long mod) {
    if (value < 0) {
      value += mod;
    }
  }
  static void softDown(long long& value, long long mod) {
    if (value >= mod) {
      value -= mod;
    }
  }
  static void hardDown(long long& value, long long mod) { value %= mod; }
  static void hard(long long& value, long long mod) {
    value %= mod;
    softUp(value, mod);
  }
};
template <>
struct impl__Normalizator<false> {
  static void softUp(long long&, long long) {}
  static void softDown(long long&, long long) {}
  static void hardDown(long long&, long long) {}
  static void hard(long long&, long long) {}
};
template <typename T>
T extendedGcd(T a, T b, T& x, T& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  T d = extendedGcd(b % a, a, y, x);
  x -= (b / a) * y;
  return d;
}
template <long long mod, bool autoNormalize = true>
class Zn {
  static_assert(mod > 0, "Mod has to be positive integer");

 public:
  Zn() : value(0) {}
  static Zn valueOf(long long value) {
    impl__Normalizator<true>::hard(value, mod);
    return Zn(value);
  }
  Zn& operator+=(const Zn& rhs) {
    value += rhs.value;
    impl__Normalizator<autoNormalize>::softDown(value, mod);
    return *this;
  }
  Zn& operator+=(long long rhs) { return *this += Zn::valueOf(rhs); }
  Zn& operator-=(const Zn& rhs) {
    value -= rhs.value;
    impl__Normalizator<autoNormalize>::softUp(value, mod);
    return *this;
  }
  Zn& operator-=(long long rhs) { return *this -= Zn::valueOf(rhs); }
  Zn& operator*=(const Zn& rhs) {
    value *= rhs.value;
    impl__Normalizator<autoNormalize>::hardDown(value, mod);
    return *this;
  }
  Zn& operator*=(long long rhs) { return *this *= Zn::valueOf(rhs); }
  void normalize() const {
    long long& value = const_cast<long long&>(this->value);
    impl__Normalizator<!autoNormalize>::hard(value, mod);
  }
  Zn operator-() const {
    Zn result(mod - value);
    impl__Normalizator<autoNormalize>::softDown(result.value, mod);
    return result;
  }
  Zn& operator/=(const Zn& rhs) { return *this *= rhs.inversed(); }
  Zn& operator/=(long long rhs) { return *this /= Zn::valueOf(rhs); }
  bool operator==(const Zn& rhs) const {
    normalize();
    rhs.normalize();
    return value == rhs.value;
  }
  Zn inversed() const {
    ;
    normalize();
    long long x, y;
    long long gcd = extendedGcd(value, mod, x, y);
    (void)gcd;
    ;
    impl__Normalizator<autoNormalize>::softUp(x, mod);
    return Zn(x);
  }
  template <long long m, bool aF>
  friend std::ostream& operator<<(std::ostream&, const Zn<m, aF>& zn);
  template <long long m, bool aF>
  friend std::istream& operator>>(std::istream&, Zn<m, aF>& zn);
  long long longValue() const { return value; }

 private:
  explicit Zn(long long value) : value(value) {}
  long long value;
};
template <long long m, bool a>
bool operator==(const Zn<m, a>& lhs, long long rhs) {
  return lhs == Zn<m, a>::valueOf(rhs);
}
template <long long m, bool a>
bool operator==(long long lhs, const Zn<m, a>& rhs) {
  return rhs == lhs;
}
template <long long m, bool a>
bool operator!=(const Zn<m, a>& lhs, const Zn<m, a>& rhs) {
  return !(lhs == rhs);
}
template <long long m, bool a>
bool operator!=(const Zn<m, a>& lhs, long long rhs) {
  return !(lhs == rhs);
}
template <long long m, bool a>
bool operator!=(long long lhs, const Zn<m, a>& rhs) {
  return !(lhs == rhs);
}
template <long long m, bool a>
Zn<m, a> operator+(const Zn<m, a>& lhs, const Zn<m, a>& rhs) {
  Zn<m, a> copy = lhs;
  return copy += rhs;
}
template <long long m, bool a>
Zn<m, a> operator+(const Zn<m, a>& lhs, long long rhs) {
  Zn<m, a> copy = lhs;
  return copy += rhs;
}
template <long long m, bool a>
Zn<m, a> operator+(long long lhs, const Zn<m, a>& rhs) {
  return rhs + lhs;
}
template <long long m, bool a>
Zn<m, a> operator-(const Zn<m, a>& lhs, const Zn<m, a>& rhs) {
  Zn<m, a> copy = lhs;
  return copy -= rhs;
}
template <long long m, bool a>
Zn<m, a> operator-(const Zn<m, a>& lhs, long long rhs) {
  Zn<m, a> copy = lhs;
  return copy -= rhs;
}
template <long long m, bool a>
Zn<m, a> operator-(long long lhs, const Zn<m, a>& rhs) {
  return Zn<m, a>::valueOf(lhs) - rhs;
}
template <long long m, bool a>
Zn<m, a> operator*(const Zn<m, a>& lhs, const Zn<m, a>& rhs) {
  Zn<m, a> copy = lhs;
  return copy *= rhs;
}
template <long long m, bool a>
Zn<m, a> operator*(const Zn<m, a>& lhs, long long rhs) {
  Zn<m, a> copy = lhs;
  return copy *= rhs;
}
template <long long m, bool a>
Zn<m, a> operator*(long long lhs, const Zn<m, a>& rhs) {
  return rhs * lhs;
}
template <long long m, bool a>
Zn<m, a> operator/(const Zn<m, a>& lhs, const Zn<m, a>& rhs) {
  Zn<m, a> copy = lhs;
  return copy /= rhs;
}
template <long long m, bool a>
Zn<m, a> operator/(const Zn<m, a>& lhs, long long rhs) {
  Zn<m, a> copy = lhs;
  return copy /= rhs;
}
template <long long m, bool a>
Zn<m, a> operator/(long long lhs, const Zn<m, a>& rhs) {
  return Zn<m, a>::valueOf(lhs) / rhs;
}
template <long long m, bool a>
std::ostream& operator<<(std::ostream& stream, const Zn<m, a>& zn) {
  zn.normalize();
  return stream << zn.value;
}
template <long long m, bool a>
std::istream& operator>>(std::istream& stream, Zn<m, a>& zn) {
  stream >> zn.value;
  impl__Normalizator<a>::hard(zn.value, m);
  return stream;
}
template <long long m, bool a>
struct impl__IdentityHelper<Zn<m, a>> {
  static Zn<m, a> identity() { return Zn<m, a>::valueOf(1); }
};
template <typename T>
T binpow(T base, long long power) {
  ;
  T result = identity<T>(base);
  ;
  while (power > 0) {
    if (power & 1) {
      result *= base;
    }
    power >>= 1;
    base *= base;
  }
  return result;
}
using namespace std;
class TaskC {
 public:
  vector<vector<pair<int, int>>> g;
  void solve(std::istream& in, std::ostream& out) {
    int n, m;
    in >> n >> m;
    g.resize(n);
    for (int i : range(m)) {
      int a, b, c;
      in >> a >> b >> c;
      --a, --b;
      g[a].emplace_back(b, 1 - c);
      g[b].emplace_back(a, 1 - c);
    }
    vector<int> colors(n, -1);
    int components = 0;
    for (int i : range(n)) {
      if (colors[i] == -1) {
        ++components;
        if (!dfs(i, 0, colors)) {
          out << 0;
          return;
        }
      }
    }
    out << binpow(Zn<1000000007>::valueOf(2), components - 1);
  }
  bool dfs(int v, int color, vector<int>& colors) {
    if (colors[v] != -1) {
      return (colors[v] == color);
    }
    colors[v] = color;
    for (auto to : g[v]) {
      if (!dfs(to.first, color ^ to.second, colors)) return false;
    }
    return true;
  }
};
int main() {
  std::ios_base::sync_with_stdio(false);
  TaskC solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  in.tie(0);
  out << std::fixed;
  out.precision(20);
  solver.solve(in, out);
  return 0;
}
