/*
                                                                                                                                                `
                                                                                                                                                `
                                                                                                                                                `
                                                                                                                                                `
                                                                                                                                                `
                                                                                                                                                `
                                                                                                                                                `
                                                                                                                                                `
                                                                         `!!!;'                                                                 `
                                                           '%$@########################&&%:.                                                    `
                                                       .%###################################%.                                                  `
                                                    .`|######################################%                                                  `
                                                 .!@###############################################$'                                           `
                                              .!@#########################@@@#########################&;.                                       `
                                             :@##################@@@&&@@@$!!%&@@@@@@@###################&'                                      `
                                          :&################@@@&$%%||||!!:'':;!!||%|!%&@@#################%`                                    `
                                         |###############@@&&$%|!;;::::':'''':::;;!!|%%%$&&@###############$`                                   `
                                      :&#############@&$%||||!;'````````````'''```'':::'':!|$@@###############;                                 `
                                    :@#############@$$%|!;;;;:'``````...```...`...`'':::''';|%$$$&############&'                                `
                                   |##############&$$$%||!!;:'`````..````....`...```''::::;!!||$&&@@###########&'                               `
                                 !##############@$%|!!!;::'''`````...``.....`````````'''::::;;::!|%$&############|                              `
                                :@##########@&%!:'`::;;:'``....```.......  .``....````````````'':;!|%$@############;                            `
                               ;##########@%;```...`'''`...  .....    ................   .`.    `:!!!!%%%&#########|                            `
                              ;##########&|:.`'`. ..`...   .......   .....  ..  .....   ...   .'':!!:.`''!@#########|.                          `
                              !##########|'`.;!`       .  ......          ..    .      ....  .::'`. .. .`'|@########@:                          `
                              ;##########%'`'%!    ..       .  .                ..    .```.   ';:`..``.`':|@########@:                          `
                              !##########$:;$&;                     . `|%;.  `;||;::`.``';;.  .;!: `;:`';|$#########@!.                         `
                              !##########@$&@|. ...... .`':!|;` !$|%%'!##$' `|####$:`:;:!&$:. '$&!';!!||%&############;                         `
                             `$#############&:  `!!'`:;;;||%@&!'!&&$;.|##%`.|#####%`'$@$&##%` .:||;;;;%@##############!                         `
                             :@#############|. .!&@|;%@@@@@&@@#@&@#@&@############@@#####@:   .`'%@%!!&###############!                         `
                             :@############@;   :&@!;$###@|%|!$@@######################@%`    .;|!;!|%@###############!                         `
                             :@############@:   `$#$%#@@##&@################@$$$&$%|!'       `%#@|;':|&###############!                         `
                              !##########@$%:    |####@&######$;.                          ..`'`      ;@##############!                         `
                              !##########!       ;#########|.                             `;`         ;###############!                         `
                              ;##########;       `%######;                           '$###&:          `%@############&'                         `
                               !########@:       .'.       .:;`                   .|%:                   !##########&'                          `
                               !########@:                                                               ;#########@:                           `
                                !######%.     !##&;.        .                  `%&!`             '&###&' !#########!                            `
                                |#######;.%###!                 `!&###@|;;%@###%.    ';;|&$:;;`     |#%. .%###%$###!                            `
                               !#######|  `%#&'    |##&$####&:     !##########;    `%#####%.!#|.   .%@:   ;###;`%##!                            `
                                !#$!$##!   :@#!        .|###$'.    ;##|.  :&#@;    .``::`          ;@|.   :@#@: `$#|                            `
                                :&&|&##;   :&#!                   :&##;    .%##!                   !&'    ;@##!  |#!                            `
                                 |@@##@;  .`%#!                  '$##;      ;##$`                 :@!    .|##@; :&!                             `
                                 !&|&##%:'.  :&!                :@#%.        .%##;               |&!.   `|@!   `$!                              `
                                 '$%%###&;.   '&&'            `%##&'           |##@&:    ...;&&&@;     .;&#|':;$&:                              `
                                 .|######&;        '$@@@@@@@@#|                                        :&######%`                               `
                                   !#####&:                                                           '$#####@:                                 `
                                   '$#####@:                                                          ;#####$`                                  `
                                     '&####%.                 ``                                     '$#####;                                   `
                                      !#####$'                |@:  .:%|;;;;!$#####@#|               '&###$:                                     `
                                       :&####&:               ;####################@:    ':`       `%###@:                                      `
                                         ;#####@%:.                                         `'';|@######$`                                      `
                                         `$######@|`                                        !#&||@#####$`                                       `
                                          :&######@%'.                        .`''''` .';!|%@#@$$@####&:                                        `
                                           |######@|'.   ;##$!!%@##########################@:   '$####;                                         `
                                           :&#####$;''.`%#########&'             `|@$:         .%###@;                                          `
                                            :@####@;          |#@##&$;         `|%'           .|####!                                           `
                                              !####@;                                .        |####!                                            `
                                               :@###@|`         .                 `|!.       |####%.                                            `
                                                .|####!          `;$@%|!!!!!%&&@#$;.        !#####|                                             `
                                                .%####&:              '%&@@@|`             '&#####|                                             `
                                                .%#####|.                                 :&######%`                                            `
                                       :|||;`    !#######;                              .%#####@@#####@$;.                                      `
                                   `$#############&%!%#####|.                         :@####%:..'!&######@;                                     `
                                  `$#############|`..'%#######!                      ;####&;.    .'%#######!                                    `
                                .|##############@!`    .'%#########%'.          '%#####%`           :@#######@%;:.                              `
                          ;%|;. !############%::;'        `!&############@&&@#######@%'               !###########&'                            `
                         !################$'   .:'         '|&######################$:.               :@#############@@@@@@;                    `
                      ';$##&;:&###########$`                  :|$@################%:.                 !#####################@;                  `
                   '%####@$!:.  `%#########!                    .:!%&@#######@%!;'.                   |##############%!$@####!                  `
                .|#######&;'. .    |#######%.                      .':;!!!!!!;`...                   .|############|. :$@@####%.                `
                !####&$&@#@!:'.       ;#####%`                  .`';;'... ..''``.                    !##########%`    ''':$######$`             `
                ;#####@$$;`%&:.         `%####;                   .:;;:'':;||!:`                    !#########!          :&#@@####!             `
          '%######@######!   !!            :$@$`                    ...`.'::`..                   '$######@!`         '|$$&!!@#######@!.        `
     '$#########@&$$@######@:    ;: . `'    ....                      ...``.                    `$######&'           '!;`'!@######&&########$'  `
  ;############@#@@@@@$|%@###$`  `!!.;' .     :!.                                             .%#####!.            .;::&####|`..`  ..;@########!'
  |########&|;'``..`:'    ..!##%:.    ;;      `.                                            .|###|`             `'|###@&$|`  ..   .`|&&@#######%:
  |###@#@@#&|:`.`';;:;!!!;:;''!$##&;'.  .'%; .;&###&|$!                                     '$; .|$:;|`   ...;&###$'    ....      :||%&########%:
  |#######@$;!%%:```           .!@###&|`   :|'!########@#!                                ;@! :@####%'. `|@###&:         ..`:::%$$&$$$&@##@@@##|:
  |########$;::'`         .`.     `%#####%!: .. !#####%` !#%.                        !; '%; !#############&:          `::`.```. .':.  :$@&&&###|:
  !####@%:::;;:::::` .........            '.  !#######%;;';; `%#&'             ;&: `;``$@!%########&&##&|::::'.             ':.     `!%@@######|:
  !#####%;;::.       ...... ....`..  ..  '::!|&########$';%`.'` .;%:        !#$';$%|&$!&##########$'        `';;;;!&##&%||||||%;`   .:|&###@###|:
  |#######|`...      `|$&&$$!`....           ;$:  :&#@:     `'''``.   .%########&'  .%#@&$%$#####$`     ....`:%&@@@@@@&&@@!`.      .`'%@#######|'
  |######@@$:       .`..`!@&$&&&&:                ;@#######@@#$%@######&$;    '%@##%!@###@&&@####%.     `!&@@@@@$:..`..``.``...     `|&####@@##!'
  ;###&|;::!&#&%%%%:         .'::::;|$&&$%|'..;%%&#######%;$########@&#####%`   '|@###;    '%####$;::::::`            '!|%%%%%%%%%&#$:'::.'%###|'
  `%###@|;|%$%$@#&|;::::`          '|%%%%%$%|%|;';$##@|'.   |######&'     .:|$%: :&@|'.     !####%.              `::;:!$@######&%%%%%!'  ':;&##|:
  '$##%:.    :&##@####@$;.  ..                    `$|     :; !#####|      '|%:  ``    ...   !####!          .  ..:&#@@@@##@&||$;         :&####|'
  !####&@@&;        ...`.'%@##@@#@@&!`            :&&!.       !###&'  :||:.`   !%'     ``:$######@@@&&@@@&@##$;``....             |@@@@########!'
  !#######@$||!:          ';!;;;!&###&|||||||||||%&##@!`.     !###&||%!!!:   .:;`        `;;%####$;:;;;;;;;;;;;::.         '||||||&#####%!;;$##!`
  `%#&:.`:!||||||%@#@%;::;;;'                     ;@@:    .. .%###|.   `:::'  .:!;'`':'`   `$####!             ':;;;;!%@@&%%%|;`        `;%&###|:
  .%##|..       '$##@@@@@@#&:...`..              `$#$'        !##%`   ..`.`:%@@! '%@$:''. .`|####|... .........`:%###@#@@@|`        ....`%#####|:
  !###@&!       .`.```':!&##@&&&&$:              '&#%.        !#&'     ````.    .```:%&&&&&@#####@!':|$&&$&&&&&&&&&@##|`````.       !@@@#######!'
  :&#########@$||||:            `::::;;;;;!!;;;:;$###$'  .'`  |#@|.  .:;;:.                `%####%:''':::::`               `!||&##$;;;:::.  ;##|:
  .%#@$%$%%%%%$@###$::::::'.           ...`;|!::!&##@$%|||!' .%##&%%$%'        `'.         :&####!                  .':::'::%@&$%%!` .!!':;$###%:
  :&#############@|.              .  '$##################&;..!@###@#######%'.             .!#####!          .              `$#####@|``;&|!&####|:
  |####&%'   .''''':|@###@$$$$$$%;.             |####%`      .|#@;.         .;$$$%$&$$$$$$&######$:..:|%$$$$$$$&&&&@######&;'''.    ;$&########|:
  |#######@%;;`    '!|!||$#######&|!!!;;;!!!;`.!#####@: .;|!!|&#$'   `:'':;;$######################################%!|!!!!!;.  .`;|&######@####%:
  |############$!''''..';$&&&&@########################$;'!;`:@#&:``'|$@######@@@@&&&&&&&@#######@&&&&&&&&&&&@@&&&&$|:.    .`''$########@&@####%:
  |###&%$&#################@%;'  ..      `|@############$: `%@###########$`  `%%:.     . :@######|         !#####################@$%&##########%:
  |#######################################@&&@######################@@&@#######################################################################%:
*/

#define _CRT_SECURE_NO_WARNINGS

#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cctype>
#include <vector>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

inline char gc() {
    static const int L = 233333;
    static char sxd[L], *sss = sxd, *ttt = sxd;
    if (sss == ttt) {
        ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
        if (sss == ttt) {
            return EOF;
        }
    }
    return *sss++;
}

#ifndef _AT_HOME
#define dd c = gc()
#else
#define dd c = getchar()
#endif
inline char readalpha() {
    char dd;
    for (; !isalpha(c); dd);
    return c;
}

inline char readchar() {
    char dd;
    for (; c == ' '; dd);
    return c;
}

template <class T>
inline bool read(T& x) {
    bool flg = false;
    char dd;
    x = 0;
    for (; !isdigit(c); dd) {
        if (c == '-') {
            flg = true;
        } else if(c == EOF) {
            return false;
        }
    }
    for (; isdigit(c); dd) {
        x = (x * 10) + (c ^ 48);
    }
    if (flg) {
        x = -x;
    }
    return true;
}
#undef dd

template <class T>
inline void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x < 10) {
        putchar(x | 48);
        return;
    }
    write(x / 10);
    putchar((x % 10) | 48);
}

template <class T>
inline void writesp(T x) {
    write(x);
    putchar(' ');
}

template <class T>
inline void writeln(T x) {
    write(x);
    puts("");
}

namespace dfcmd {

    const int maxn = 105;
    const int maxm = 1005;
    const int mod = 31011;

    int n, m;

    struct Edge {
        int u, v, d;

        friend bool operator < (const Edge& a, const Edge& b) {
            return a.d < b.d;
        }
    } e[maxm];

    vector<pair<int, int> > v[maxn];

    int col[maxn];

    int fa[maxn];

    inline int getfa(int x) {
        return fa[x] == x ? x : fa[x] = getfa(fa[x]);
    }

    inline void dfs(int now, int ccol, int bx) {
        col[now] = ccol;
        for (auto to : v[now]) {
            if (!col[to.first] && to.second != bx) {
                dfs(to.first, ccol, bx);
            }
        }
    }

    struct Matrix {
        int mt[maxn][maxn];

        inline void init(int n) {
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= n; ++j) {
                    mt[i][j] = 0;
                }
            }
        }

        inline int* operator [] (int x) {
            return mt[x];
        }

        inline int solve(int n) {
            n--;
            if (!n) {
                return 1;
            }
            int ans = 1;
            for (int i = 1; i <= n; ++i) {
                int now = 0;
                for (int j = i; j <= n; ++j) {
                    if (mt[j][i]) {
                        now = i;
                        break;
                    }
                }
                if (!now) {
                    return 0;
                } else if (now != i) {
                    for (int j = i; j <= n; ++j) {
                        swap(mt[i][j], mt[now][j]);
                    }
                    ans *= -1;
                }
                for (int j = i + 1; j <= n; ++j) {
                    while (mt[j][i]) {
                        int nowk = mt[i][i] / mt[j][i];
                        for (int k = i; k <= n; ++k) {
                            mt[i][k] -= mt[j][k] * nowk % mod;
                            if (mt[i][k] < 0) {
                                mt[i][k] += mod;
                            } else if (mt[i][k] >= mod) {
                                mt[i][k] -= mod;
                            }
                            swap(mt[i][k], mt[j][k]);
                        }
                        ans *= -1;
                    }
                }
            }
            for (int i = 1; i <= n; ++i) {
                (ans *= mt[i][i]) %= mod;
            }
            if (ans <= mod) {
                ans += mod;
            }
            return ans;
        }
    } mat;

    inline int Main() {
        read(n), read(m);
        for (int i = 1; i <= m; ++i) {
            read(e[i].u), read(e[i].v), read(e[i].d);
        }
        sort(e + 1, e + m + 1);
        int cnt = 0, now = 0;
        for (int i = 1; i <= m; ++i) {
            if (now < e[i].d) {
                now = e[i].d;
                cnt++;
            }
            e[i].d = cnt;
        }
        for (int i = 1; i <= n; ++i) {
            fa[i] = i;
        }
        for (int i = 1; i <= m; ++i) {
            int fax = getfa(e[i].u);
            int fay = getfa(e[i].v);
            if (fax != fay) {
                fa[fax] = fay;
                v[e[i].u].emplace_back(e[i].v, e[i].d);
                v[e[i].v].emplace_back(e[i].u, e[i].d);
            }
        }
        int ans = 1;
        for (int i = 1; i <= cnt; ++i) {
            memset(col, 0, sizeof(col));
            int cntt = 0;
            for (int j = 1; j <= n; ++j) {
                if (!col[j]) {
                    dfs(j, ++cntt, i);
                    // cout << "dfs " << j << ' ' << cntt << endl;
                }
            }
            mat.init(cntt);
            for (int j = 1; j <= m; ++j) {
                // cout << "e " << col[e[j].u] << ' ' << col[e[j].v] << ' ' << e[j].d << endl;
                if (e[j].d == i && col[e[j].u] != col[e[j].v]) {
                    mat[col[e[j].u]][col[e[j].v]]--;
                    mat[col[e[j].v]][col[e[j].u]]--;
                    mat[col[e[j].u]][col[e[j].u]]++;
                    mat[col[e[j].v]][col[e[j].v]]++;
                }
            }
            /*
            for (int i = 1; i <= cntt; ++i) {
                for (int j = 1; j <= cntt; ++j) {
                    cout << mat[i][j] << ' ';
                }
                cout << endl;
            }
            cout << mat.solve(cntt) << endl;
            cout << endl;
            */
            (ans *= mat.solve(cntt)) %= mod;
        }
        writeln(ans);
        return 0;
    }

}

int main() {
    return dfcmd::Main();
}