#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long maxn = 1e6 + 10;
const char infile[] = ".in";
const char outfile[] = ".out";
inline long long read() {
  long long x(0), t(0);
  char o(getchar());
  while (o < '0' || o > '9') {
    if (o == '-') t = 1;
    o = getchar();
  }
  for (; o >= '0' && o <= '9'; o = getchar())
    x = (x << 1) + (x << 3) + (o ^ 48);
  return t ? -x : x;
}
inline void file() {
  freopen(infile, "r", stdin);
  freopen(outfile, "w", stdout);
}
inline long long Pow(long long x, long long y) {
  long long res(1);
  while (y) {
    if (y & 1) res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res % mod;
}
long long ans, fac[maxn], inv[maxn], bas[maxn];
inline long long C(long long x, long long y) {
  return fac[x] * inv[y] % mod * inv[x - y] % mod;
}
int main() {
  long long n = read();
  ans = (Pow(3, n * n) - Pow((Pow(3, n) - 3 + mod) % mod, n) + mod) % mod;
  fac[0] = inv[0] = bas[0] = 1;
  for (long long i = 1; i <= n; ++i) {
    fac[i] = fac[i - 1] * i % mod;
    bas[i] = 3 * bas[i - 1] % mod;
  }
  inv[n] = Pow(fac[n], mod - 2);
  for (long long i = n; i >= 2; --i) inv[i - 1] = inv[i] * i % mod;
  for (long long i = 1; i <= n; ++i) {
    if (i & 1)
      ans = (ans + C(n, i) *
                       (3 * Pow(bas[n - i] - 1, n) % mod +
                        (bas[i] - 3) * Pow(3, n * (n - i)) % mod) %
                       mod) %
            mod;
    else
      ans = (ans -
             C(n, i) *
                 (3 * Pow(bas[n - i] - 1, n) % mod +
                  (bas[i] - 3) * Pow(3, n * (n - i)) % mod) %
                 mod +
             mod) %
            mod;
  }
  cout << ans << endl;
}
