#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - 48;
    s = getchar();
  }
  return f * x;
}
const int p = 998244353;
const int N = 1e6 + 5;
int fac[N], inv[N];
inline int add(int a, int b) { return a + b >= p ? a + b - p : a + b; }
inline int sub(int a, int b) { return a - b < 0 ? a - b + p : a - b; }
inline int ksm(int a, long long b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % p)
    if (b & 1) ans = 1ll * ans * a % p;
  return ans;
}
inline int C(int n, int m) {
  return 1ll * fac[n] * inv[m] % p * inv[n - m] % p;
}
int main() {
  int n = read(), ans = 0;
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % p;
  inv[n] = ksm(fac[n], p - 2);
  for (int i = n - 1; i >= 1; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % p;
  for (int i = 1; i <= n; i++)
    ans = add(ans, 1ll * ksm(3, (1ll * n * (n - i) + i)) * ksm(p - 1, i + 1) %
                       p * C(n, i) % p);
  ans = add(ans, ans);
  int res = 0;
  for (int i = 0; i < n; i++) {
    int t = sub(0, ksm(3, i));
    res = add(res, 1ll * C(n, i) * ksm(p - 1, i + 1) % p *
                       sub(ksm(1 + t, n), ksm(t, n)) % p);
  }
  ans = add(ans, 3ll * res % p);
  printf("%d\n", ans);
  return 0;
}
