#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const long long MOD = 998244353;
struct LL {
  long long v;
  LL(long long v = 0) : v(v) {}
  LL operator+(LL o) {
    LL r(v + o.v);
    if (r.v >= MOD) r.v -= MOD;
    return r;
  }
  LL operator-(LL o) {
    LL r(v - o.v);
    if (r.v < 0) r.v += MOD;
    return r;
  }
  LL operator*(LL o) {
    LL r(v * o.v % MOD);
    return r;
  }
  LL operator/(LL o) {
    LL r(v * o.inv().v % MOD);
    return r;
  }
  LL pow(long long e) {
    LL r(1), b(v);
    while (e)
      if (e & 1)
        r = r * b, e ^= 1;
      else
        b = b * b, e >>= 1;
    return r;
  }
  LL inv() { return pow(MOD - 2); }
};
LL fac[N];
long long n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0] = 1;
  for (int i = 1, qwerty = N; i < qwerty; i++) fac[i] = LL(i) * fac[i - 1];
  LL ans(0);
  for (int i = 1, qwerty = n + 1; i < qwerty; i++) {
    LL tmp1 = (LL(3).pow(n - i) - LL(1)).pow(n),
       tmp2 = LL(3).pow((n - 1LL * i) * n);
    LL cmb = fac[n] / (fac[i] * fac[n - i]);
    ans = ans + LL(i & 1 ? 1 : MOD - 1) * LL(3) * cmb * (tmp1 - tmp2);
  }
  for (int i = 1, qwerty = n + 1; i < qwerty; i++) {
    LL cmb = fac[n] / (fac[i] * fac[n - i]);
    ans = ans + LL(2) * LL(i & 1 ? 1 : MOD - 1) *
                    LL(3).pow(n * (n - 1LL * i) + 1LL * i) * cmb;
  }
  cout << ans.v << "\n";
}
